Welcome to Day 27 of the 100 Days of Python series!
Today, we’re diving into nested data structures — where things get structured, organized, and powerful.
When building real-world applications like APIs, databases, or configuration files, you’ll often use lists within dictionaries, dictionaries within lists, and more. Python makes this nesting easy and flexible.
Let’s explore how to create, access, and manipulate nested data structures like a pro. 🐍💼
📦 What You’ll Learn
- What nested data structures are
- How to build combinations like
list in dict
,dict in list
, etc. - How to access deeply nested values
- Best practices and real-world examples
🔄 What Are Nested Data Structures?
A nested data structure is simply one data structure inside another, like a list inside a dictionary or a dictionary inside a list.
🔹 1. List of Dictionaries
users = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30},
{"name": "Charlie", "age": 22}
]
🔸 Accessing Items
print(users[0]["name"]) # Alice
Looping:
for user in users:
print(user["name"])
🔹 2. Dictionary of Lists
grades = {
"math": [90, 85, 88],
"science": [92, 89, 94]
}
🔸 Accessing Values
print(grades["math"][1]) # 85
Adding a new grade:
grades["math"].append(95)
🔹 3. Dictionary of Dictionaries
users = {
"alice": {"email": "alice@example.com", "age": 25},
"bob": {"email": "bob@example.com", "age": 30}
}
🔸 Accessing Nested Values
print(users["bob"]["email"]) # bob@example.com
Modifying:
users["bob"]["age"] = 31
🔹 4. List of Lists
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
🔸 Accessing Elements
print(matrix[1][2]) # 6
Looping:
for row in matrix:
for val in row:
print(val, end=" ")
📊 Real-World Examples
📋 1. JSON-like API Response
response = {
"status": "success",
"data": {
"users": [
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"}
]
}
}
Accessing Bob’s name:
print(response["data"]["users"][1]["name"]) # Bob
🧠 2. Quiz System Example
quiz = [
{
"question": "What is 2 + 2?",
"options": [2, 3, 4, 5],
"answer": 4
},
{
"question": "What is the capital of France?",
"options": ["London", "Berlin", "Paris", "Rome"],
"answer": "Paris"
}
]
Loop through questions:
for q in quiz:
print(q["question"])
print("Options:", q["options"])
✅ Best Practices for Nested Structures
- ✅ Use
.get()
to safely access nested keys:
user.get("profile", {}).get("email", "Not Found")
- ✅ Use readable variable names when unpacking:
for user in users:
name = user["name"]
age = user["age"]
- ✅ Keep nesting shallow when possible — deep nesting can make your code harder to maintain.
🧪 Bonus: Modifying Nested Structures
Updating a specific value:
users[1]["age"] = 35
Appending a new item to a nested list:
grades["science"].append(96)
🧠 Recap
Today you learned:
- How to build and use nested data structures
- Common combinations like lists of dicts and dicts of lists
- How to access and modify deeply nested elements
- Real-world use cases including API responses and quiz apps
- Best practices for working with nested structures
Top comments (0)