Codzy

Dictionaries

A dictionary stores values linked to keys, between curly braces: {"name": "Sara", "age": 20}. You read a value with its key: student["name"].

To add or change a value, write d[key] = value. .keys() gives the keys, .values() the values and .items() the key-value pairs, which are handy in a for loop.

student = {"name": "Sara", "age": 20}
print(student["name"])
student["city"] = "Paris"
print(len(student))
for key, value in student.items():
    print(key, "->", value)

Start the lesson