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.
|
|
Dictionaries of Dictionaries
Useful when you want to look up an item by a unique key (like a username or ID).
|
|
Iterating through Nested Data
To access everything, we often use nested loops.
|
|
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.
Discussion
Have questions or want to discuss this lesson? Join the conversation below!