Nested Data Structures in Python
Python's collections can hold other collections — a list of dictionaries, a dictionary of lists, or any combination — which is how most real-world structured data (JSON responses, spreadsheets, configuration files) gets represented. Accessing a deeply nested value just means chaining indices and keys together, one level at a time.
A list can hold dictionaries
[{"name": "Ada"}, {"name": "Grace"}] is a common shape for a list of records.
A dictionary can hold lists
{"scores": [90, 85, 88]} nests a list as one value.
Chain brackets outside-in
data["scores"][0] or data[0]["name"] steps through each level in turn.
Nesting can go arbitrarily deep
A list of dictionaries of lists of dictionaries, if the data genuinely calls for it.
Looping usually means a loop inside a loop
One loop for each level of nesting being visited.
students = [ {"name": "Ada", "scores": [90, 85]}, {"name": "Grace", "scores": [95, 92]},] print(students[0]["name"])print(students[1]["scores"][0]) for student in students: print(student["name"], sum(student["scores"]))Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.