Skip to main content

Nested Collections

beginner10 min readLesson 19 of 169

Lists of dicts, dicts with lists โ€” the shape of real data.

Containers nest โ€” lists inside dicts, dicts in lists โ€” and that combination is how real data looks:

students = [
    {"name": "Minh", "grades": [8, 9, 7]},
    {"name": "Lan", "grades": [10, 9]},
]

students[0]["name"]           # 'Minh'
students[1]["grades"][-1]     # 9
students[0]["grades"].append(6)

Read nested access inside-out, one bracket at a time. When it gets confusing, print the intermediate value:

first = students[0]
print(first)       # see the whole dict before digging deeper

Nested containers are the bridge to everything ahead: files (Module 9 saves them as JSON), modules (Module 10 groups functions), and every real dataset you will ever process.

Now practice

Mini Build: Nested DataRead and mutate records the way real apps do.3 challenges ยท ยท ~35 min