DEV Community

Cover image for Day 27/100: Nested Data Structures in Python
 Rahul Gupta
Rahul Gupta

Posted on

Day 27/100: Nested Data Structures in Python

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}
]
Enter fullscreen mode Exit fullscreen mode

🔸 Accessing Items

print(users[0]["name"])  # Alice
Enter fullscreen mode Exit fullscreen mode

Looping:

for user in users:
    print(user["name"])
Enter fullscreen mode Exit fullscreen mode

🔹 2. Dictionary of Lists

grades = {
    "math": [90, 85, 88],
    "science": [92, 89, 94]
}
Enter fullscreen mode Exit fullscreen mode

🔸 Accessing Values

print(grades["math"][1])  # 85
Enter fullscreen mode Exit fullscreen mode

Adding a new grade:

grades["math"].append(95)
Enter fullscreen mode Exit fullscreen mode

🔹 3. Dictionary of Dictionaries

users = {
    "alice": {"email": "alice@example.com", "age": 25},
    "bob": {"email": "bob@example.com", "age": 30}
}
Enter fullscreen mode Exit fullscreen mode

🔸 Accessing Nested Values

print(users["bob"]["email"])  # bob@example.com
Enter fullscreen mode Exit fullscreen mode

Modifying:

users["bob"]["age"] = 31
Enter fullscreen mode Exit fullscreen mode

🔹 4. List of Lists

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
Enter fullscreen mode Exit fullscreen mode

🔸 Accessing Elements

print(matrix[1][2])  # 6
Enter fullscreen mode Exit fullscreen mode

Looping:

for row in matrix:
    for val in row:
        print(val, end=" ")
Enter fullscreen mode Exit fullscreen mode

📊 Real-World Examples

📋 1. JSON-like API Response

response = {
    "status": "success",
    "data": {
        "users": [
            {"id": 1, "name": "Alice"},
            {"id": 2, "name": "Bob"}
        ]
    }
}
Enter fullscreen mode Exit fullscreen mode

Accessing Bob’s name:

print(response["data"]["users"][1]["name"])  # Bob
Enter fullscreen mode Exit fullscreen mode

🧠 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"
    }
]
Enter fullscreen mode Exit fullscreen mode

Loop through questions:

for q in quiz:
    print(q["question"])
    print("Options:", q["options"])
Enter fullscreen mode Exit fullscreen mode

✅ Best Practices for Nested Structures

  • ✅ Use .get() to safely access nested keys:
  user.get("profile", {}).get("email", "Not Found")
Enter fullscreen mode Exit fullscreen mode
  • ✅ Use readable variable names when unpacking:
  for user in users:
      name = user["name"]
      age = user["age"]
Enter fullscreen mode Exit fullscreen mode
  • ✅ 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
Enter fullscreen mode Exit fullscreen mode

Appending a new item to a nested list:

grades["science"].append(96)
Enter fullscreen mode Exit fullscreen mode

🧠 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)