Dictionaries

A dictionary is a data type similar to arrays, but instead works with keys and values instead of indexes. Each value that you store in a dictionary can be accessed using a key, which is any type of object, as opposted to using its index to address it.

A database of student scores can be stored in a dictionary like so:

scores = {}

scores["Bob"] = 10

scores["Brian"] = 5

scores["James"] = 0

However a better way would be to organise it like so if you already know the data:

scores = {

"Bob" : 10,

"Brian" : 5,

"James" : 0

}

print(scores)

Iterating over dictionaries
Dictionaries can be iterated over but the order of the values are not stored in it. The following syntax is to be used to iterate over key value pairs:

for name, score in scores.items:

print("Score of %s is %d"%(name, score))

Removing a value
To remove an index, use either:

del score["Bob"]

or:

score.pop("Bob")

Continue to the next lesson.