- Courses
- 💻Programming Languages
- 🐍Python
- Core Data Structures
- Nested Structures
On this page
Nested Structures
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
What Nested Structures Are
A nested structure is any data structure that contains another data structure inside it. Python allows you to combine lists, tuples, dictionaries, and sets in flexible ways, enabling you to model complex, real‑world data.
Examples of nested structures include:
- A list of lists
- A dictionary containing lists
- A list of dictionaries
- A dictionary of dictionaries
Nested structures are essential for representing JSON data, API responses, configuration files, and multi‑dimensional datasets.
Why Nested Structures Matter
Nested structures allow you to:
- Organize related data hierarchically
- Represent complex objects cleanly
- Build multi‑level models (users → addresses → preferences)
- Work effectively with external data formats such as JSON
They are foundational for intermediate and advanced Python programming.
Lists Inside Lists
A list can contain other lists, forming a multi‑dimensional structure.
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Accessing Nested List Elements
Use multiple indices:
print(matrix[1][2]) # 6
This pattern is common in grid‑based data, tables, and numerical computations.
Dictionaries Inside Lists
A list of dictionaries is one of the most common patterns in Python, especially when processing structured data.
users = [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25}
]
Accessing Values
print(users[0]["name"]) # Alice
This structure mirrors JSON arrays returned by APIs.
Lists Inside Dictionaries
Dictionaries often store lists as values.
student = {
"name": "Sara",
"scores": [85, 90, 92]
}
Accessing Nested Values
print(student["scores"][1]) # 90
This pattern is ideal for representing entities with multiple attributes.
Dictionaries Inside Dictionaries
Nested dictionaries allow you to build multi‑level structures.
config = {
"database": {
"host": "localhost",
"port": 5432
}
}
Accessing Nested Keys
print(config["database"]["host"])
This is the basis of configuration files and hierarchical data.
Tuples Inside Other Structures
Tuples are often used to store fixed‑size records inside lists or dictionaries.
points = [
(10, 20),
(30, 40)
]
Tuples ensure that coordinate pairs remain unchanged.
Sets Inside Other Structures
Sets can appear inside lists or dictionaries, but cannot contain mutable elements such as lists or dictionaries.
Valid:
groups = {
"admins": {"alice", "bob"},
"guests": {"tom"}
}
Invalid:
# {"a", ["b", "c"]} # lists cannot be inside sets
Sets are ideal for fast membership testing within nested structures.
Looping Through Nested Structures
Nested loops allow you to process multi‑level data.
Example: Looping Through a List of Dictionaries
for user in users:
print(user["name"], user["age"])
Example: Looping Through a Matrix
for row in matrix:
for value in row:
print(value)
Nested loops are essential for traversing hierarchical data.
Modifying Nested Structures
Because lists and dictionaries are mutable, you can update nested values easily.
users[1]["age"] = 26
matrix[0][1] = 99
Always ensure you understand the structure before modifying it.
When to Use Nested Structures
Use nested structures when:
- Modeling real‑world entities with multiple attributes
- Working with JSON or API responses
- Representing multi‑dimensional data
- Organizing hierarchical or grouped information
- Building configuration or settings systems
Nested structures provide clarity, flexibility, and expressive power.
Summary
Nested structures combine Python’s core data types—lists, tuples, dictionaries, and sets—to represent complex, layered information. They are essential for real‑world programming, data processing, and interacting with modern APIs. Mastering nested structures prepares you for advanced topics such as data modeling, serialization, and working with external data formats.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Sets
Next
Iterating Over Complex Data
-
What Nested Structures Are
-
Why Nested Structures Matter
-
Lists Inside Lists
- Accessing Nested List Elements
-
Dictionaries Inside Lists
- Accessing Values
-
Lists Inside Dictionaries
- Accessing Nested Values
-
Dictionaries Inside Dictionaries
- Accessing Nested Keys
-
Tuples Inside Other Structures
-
Sets Inside Other Structures
-
Looping Through Nested Structures
- Example: Looping Through a List of Dictionaries
- Example: Looping Through a Matrix
-
Modifying Nested Structures
-
When to Use Nested Structures
-
Summary
