Lesson 3: Dictionaries & Structured Data

⏱ ~25 min Lesson 3 of 10 💚 Free

A dictionary stores data as key-value pairs — like a real dictionary where each word (key) has a definition (value). Dictionaries are perfect for representing structured records like a student profile or a row of a spreadsheet.

Key Concepts

Creating Dictionaries

student = {"name": "Alex", "grade": 8, "score": 92}
Keys are unique labels (strings, numbers). Values can be anything. Access: student["name"] → "Alex".

Adding & Updating

student["city"] = "Tulsa" # adds new key
student["score"] = 95 # updates existing key
del student["city"] # removes a key

Looping Through Dicts

for key, value in student.items():
print(key, ":", value)
Use .keys() for just keys, .values() for just values, .items() for both.

List of Dictionaries

A list of dictionaries represents a table of records — exactly like a CSV file or spreadsheet:
students = [
{'name':'Alex','score':92},
{'name':'Brooke','score':85}
]

🔬 Interactive Lab: Student Record Builder

Add name and score entries. The table updates live and shows the class average.

✅ Check Your Understanding

1. How do you access the value for key "name" in dict d?

2. What method gives you both keys and values when looping?

3. A list of dictionaries is similar to: