Day 19 45 min beginner

Nested Data Structures

Learn how to combine lists and dictionaries to model complex real-world data.

Learning Objectives

  • Create and access data in nested lists
  • Work with lists of dictionaries
  • Understand how to model complex information
  • Iterate through multi-level structures

Modeling the Real World

In simple programs, a list of names is enough. But what if you are building an app for a school? You need to store students, and for each student, you need their name, age, and a list of their grades.

To handle this, we nest data structures inside each other.

Lists of Dictionaries

This is the most common way to store a “database” of items in Python.

students.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
students = [
    {"name": "Alice", "age": 20, "grades": [85, 90, 88]},
    {"name": "Bob", "age": 22, "grades": [70, 75, 80]},
    {"name": "Charlie", "age": 21, "grades": [95, 98, 92]}
]

# Accessing Bob's age
print(students[1]["age"]) # 22

# Accessing Alice's first grade
print(students[0]["grades"][0]) # 85

Dictionaries of Dictionaries

Useful when you want to look up an item by a unique key (like a username or ID).

users.py
1
2
3
4
5
6
users = {
    "jdoe": {"full_name": "John Doe", "active": True},
    "asmith": {"full_name": "Anne Smith", "active": False}
}

print(users["jdoe"]["full_name"]) # John Doe

Iterating through Nested Data

To access everything, we often use nested loops.

iteration.py
1
2
3
4
5
for student in students:
    print(f"Name: {student['name']}")
    # Nested loop to print grades
    for grade in student['grades']:
        print(f"  - Grade: {grade}")

Interactive Practice

Think of a menu for a restaurant. You might have a dictionary where keys are categories ("Appetizers", "Main Course") and values are lists of dish names.

Concept Example Access Logic
List of Lists matrix[0][1] [row][column]
List of Dicts users[0]['id'] [index]['key']
Dict of Lists catalog['books'][0] ['key'][index]

Quiz

Complete this quiz with a minimum score of 80% to mark Day 19 as complete.

Loading quiz...

Discussion

Have questions or want to discuss this lesson? Join the conversation below!