Nested Data Structures
Nested Data Structures
Sometimes one data structure is not enough.
Real programs often store complex data such as:
- A student with many subjects
- A product with many details
- A class with many students
- A store with many products
To do this, Python allows one data structure inside another.
This is called nesting.
What Does Nested Mean?
Nested means placed inside something else.
Examples:
- A list inside a list
- A dictionary inside a dictionary
- A list inside a dictionary
- A dictionary inside a list
Why Nested Data Matters
Nested data helps you organize real information in a clean way.
Instead of many random variables, related data stays together.
List Inside a List
numbers = [
[1, 2, 3],
[4, 5, 6]
]
print(numbers)
Output
[[1, 2, 3], [4, 5, 6]]
Access Nested List Items
Use two indexes.
print(numbers[0][1])
print(numbers[1][2])
Output
2
6
Explanation:
numbers[0]= first inner listnumbers[0][1]= second item inside first list
Dictionary Inside a Dictionary
user = {
"name": "Tom",
"address": {
"city": "New York",
"country": "USA"
}
}
print(user["address"]["city"])
Output
New York
List Inside a Dictionary
student = {
"name": "Sara",
"scores": [80, 90, 75]
}
print(student["scores"])
print(student["scores"][1])
Output
[80, 90, 75]
90
Dictionary Inside a List
students = [
{"name": "Tom", "score": 85},
{"name": "Sara", "score": 92}
]
print(students[0]["name"])
print(students[1]["score"])
Output
Tom
92
Updating Nested Data
You can change values inside nested data.
student = {
"name": "Sara",
"scores": [80, 90, 75]
}
student["scores"][0] = 100
print(student)
Output
{'name': 'Sara', 'scores': [100, 90, 75]}
Loop Through Nested Data
students = [
{"name": "Tom", "score": 85},
{"name": "Sara", "score": 92}
]
for student in students:
print(student["name"], student["score"])
Output
Tom 85
Sara 92
Real Example: Store Products
products = [
{"name": "Laptop", "price": 900},
{"name": "Phone", "price": 500}
]
for product in products:
print(product["name"], product["price"])
Output
Laptop 900
Phone 500
Code Along
classroom = {
"teacher": "Maya",
"students": ["Tom", "Sara", "John"]
}
print(classroom["teacher"])
print(classroom["students"][0])
Output
Maya
Tom
Mini Challenge
Build a movie collection.
Steps:
- Create a list of dictionaries with 2 movies
- Each movie should have:
title,year - Print the first movie title
- Print the second movie year
- Loop through all movies and print title and year
Expected output:
Inception
2020
Inception 2010
Avatar 2020
Real World Use Case
Programs use nested data for APIs, shopping carts, user profiles, dashboards, games, and databases.
Quiz
- What does nested mean?
- How do you access a list inside a dictionary?
- Can dictionaries be stored in a list?
- Why is nested data useful?
Assignment
Create a dictionary for a school. Add a list of students inside it. Print the school name and all student names.
Summary
You learned how Python stores data structures inside other data structures and how nested data is used in real programs.