Dictionaries
A dictionary keeps related pieces of information under named keys. A student's name and age can be stored together and read by their labels instead of positions.
By the end of this lesson, you can:
- Create key/value pairs
- Read and update a value
- Add a new key
- Check keys and loop through pairs
Dictionaries lesson progress0%
Course progress: 0% · 0 of 12 steps completed in this module
Learn · Pair keys with values
Pair keys with values
Inside braces, each key is followed by a colon and a value. Keys such as 'name' describe what a value means. This is different from a list, where positions are numbers.
Pattern
student = {"name": "Adam", "age": 18}Code
student = {"name": "Adam", "age": 18}
print(student["name"])Output
Adam
Why it works
- name and age are keys.
- Adam and 18 are their values.
- Square brackets with a key read one value.
Common mistake
Not this
student[0] # expecting nameTry this
student["name"]A dictionary is read by key, not list position.