Learn Python
>>> data_structures.nested_structures

Nested Data Structures in Python

#concept

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.

#how it works
1

A list can hold dictionaries

[{"name": "Ada"}, {"name": "Grace"}] is a common shape for a list of records.

2

A dictionary can hold lists

{"scores": [90, 85, 88]} nests a list as one value.

3

Chain brackets outside-in

data["scores"][0] or data[0]["name"] steps through each level in turn.

4

Nesting can go arbitrarily deep

A list of dictionaries of lists of dictionaries, if the data genuinely calls for it.

5

Looping usually means a loop inside a loop

One loop for each level of nesting being visited.

#example
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"]))
Output
Ada
95
Ada 175
Grace 187
1students[0]["name"] steps into the first dictionary in the list, then looks up its "name" key.
2students[1]["scores"][0] steps into the second dictionary, then its "scores" list, then the first item in that list.
3The loop goes through each dictionary in students, printing the name alongside the sum of that student's scores.
#use cases
Representing data from a JSON API response, which is almost always nested dictionaries and lists
Modeling real-world records, like a list of orders each containing a list of items
Configuration files with grouped settings, like a dictionary of dictionaries
Any data with a natural hierarchy — categories containing items, folders containing files

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.