← Module 4 Module 6 →

📖 Module 5

Dictionaries

Lists are ordered piles. Dictionaries are labeled drawers: each key unlocks one value.

A magical cabinet of key-shaped drawers with a coder and a raccoon trying keys.
Rowan labels everything. The dragon’s favorite key is "snack_location".

🪄 The concept

A dictionary stores key → value pairs with curly braces: {"name": "Harry", "house": "Gryffindor"}.

Look up with student["name"], update with assignment, and loop with .items(). Use .get(key, default) when a key might be missing.

Dictionaries scale up: nested dicts (a roster of students), spell books, and — later — JSON from the internet all feel like this same idea.

💬 Discussion

Talk these through with a friend, a class, or just your notes app:

  1. Would you store a class seating chart as a list or a dictionary? Why?
  2. What makes a good key? (Hint: keys should be unique and stable.)
  3. How is a contact app on your phone basically a dictionary?

💻 Code walkthrough

Copy into your editor, or download the .py file and run it. Tinker — change names, numbers, and spells.

Dictionary basics student_record.py

# Keys label values — like a student record or spell book entry
student = {
    "name": "Harry",
    "house": "Gryffindor",
    "points": 120
}

# Access by key; update with assignment
print(student["name"], "has", student["points"], "points")
student["points"] = student["points"] + 10
print("After winning the match:", student["points"])

Looping through keys and values house_points.py

# House point totals — keys are houses, values are scores
house_points = {
    "Gryffindor": 350,
    "Ravenclaw": 310,
    "Hufflepuff": 290,
    "Slytherin": 275
}

for house, points in house_points.items():
    print(house + ":", points, "points")

# get() returns a default if the key is missing (safer than [])
bonus = house_points.get("Beauxbatons", 0)
print("Beauxbatons points (if any):", bonus)

Nested dictionaries (dict inside a dict) nested_roster.py

# Store several students — each value is another dictionary
roster = {
    "harry": {"house": "Gryffindor", "year": 5},
    "luna": {"house": "Ravenclaw", "year": 4}
}

print(roster["luna"]["house"])   # two keys in a row

roster["harry"]["points"] = 50
print("Harry now has", roster["harry"]["points"], "points")

Spell book — keys as spell names spell_book.py

spell_book = {
    "Lumos": "Creates light at wand tip",
    "Nox": "Extinguishes light",
    "Accio": "Summons an object"
}

# Loop through spell names only
for spell_name in spell_book.keys():
    print(spell_name, "—", spell_book[spell_name])

🧪 Try this

  1. Create a dictionary for yourself: name, house (pick one), favorite spell. Print a greeting that uses all three keys.
  2. Extend spell_book.py with two new spells. Print only the descriptions (values), not the names.
  3. Add a third student to nested_roster.py. Print their house and year with chained keys.

📁 Local files

All runnable Python for this module:

module-05-dictionaries/ · all modules

🎬 Video lesson

← Module 4 Module 6 →