Ojasa Mirai

Ojasa Mirai

Python

Loading...

Learning Level

🟢 Beginner🔵 Advanced
🎯 Why Data Structures Matter📋 Lists: Ordered Collections🔑 Dictionaries: Key-Value Pairs🎪 Sets: Unique Values & Speed📦 Tuples: Immutable Sequences⚖️ Comparing All Data Structures🔧 Mastering List Methods⚡ Advanced Dictionary Patterns🔄 Power of Set Operations🏗️ Building Nested Structures⚙️ Performance Tuning & Optimization
Python/Data Structures/Nested Structures

🏗️ Nested Data Structures — Organizing Complex Data

Combine lists, dictionaries, and tuples to create powerful structures for real-world data.


🎯 Lists Inside Dictionaries

Store multiple values for each key using lists.

# Student database with multiple grades
students = {
    "Alice": {"grades": [95, 87, 92], "major": "CS"},
    "Bob": {"grades": [78, 82, 80], "major": "Math"},
    "Carol": {"grades": [88, 91, 89], "major": "Physics"}
}

# Access nested data
alice_grades = students["Alice"]["grades"]
print(alice_grades)  # [95, 87, 92]
print(alice_grades[0])  # 95 (first grade)

💡 Dictionaries Inside Lists

Store structured data for multiple items.

# List of products with details
products = [
    {"name": "Laptop", "price": 1000, "stock": 5},
    {"name": "Mouse", "price": 25, "stock": 50},
    {"name": "Keyboard", "price": 75, "stock": 30}
]

# Access and modify
for product in products:
    print(f"{product['name']}: ${product['price']}")

# Change stock
products[0]["stock"] = 4

🎨 Real-World Example: Social Network Profile

# Store user profile with nested data
user = {
    "name": "Alice Johnson",
    "age": 28,
    "email": "alice@email.com",
    "location": {"city": "New York", "country": "USA"},
    "hobbies": ["reading", "coding", "hiking"],
    "posts": [
        {"title": "My first post", "likes": 5},
        {"title": "Python tips", "likes": 12},
        {"title": "Web development", "likes": 8}
    ]
}

# Access various levels
print(user["name"])                    # Alice Johnson
print(user["location"]["city"])        # New York
print(user["hobbies"][0])              # reading
print(user["posts"][1]["likes"])       # 12

📊 Nested Structure Patterns

# List of dictionaries (rows in a table)
data = [
    {"id": 1, "name": "Item A", "price": 100},
    {"id": 2, "name": "Item B", "price": 200}
]

# Dictionary of lists (columns)
inventory = {
    "apples": [5, 3, 8],
    "oranges": [2, 4, 6]
}

# Mixed nesting (most flexible)
config = {
    "database": {
        "host": "localhost",
        "ports": [3306, 5432]
    },
    "features": ["auth", "api", "cache"]
}

🔑 Key Takeaways

  • ✅ Nest lists inside dictionaries for multiple values per key
  • ✅ Nest dictionaries inside lists for structured items
  • ✅ Use multiple bracket levels to access deep data: `data[key][index][subkey]`
  • ✅ Always check structure before accessing to avoid errors
  • ✅ Real-world data is almost always nested

Ready to practice? Challenges | Quiz


Resources

Python Docs

Ojasa Mirai

Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.

Learn Deep • Build Real • Verify Skills • Launch Forward

Courses

PythonFastapiReactJSCloud

© 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn