🐍 Python for kids · Class 7 · Lesson 8 of 18 · Free
Nested Data – Lists and Dictionaries Inside Each Other
⏱ About 30 minutes · ▶ run real Python here — nothing to install · no sign-in
In this lesson you will
- Read and change items in lists of lists with two index numbers.
- Store records as a list of dictionaries and loop over them.
- Combine dictionaries and lists to model real data like timetables.
A report card has many students, and each student has many marks. One flat list cannot hold all that neatly. Real data is nested, like boxes inside boxes.
What is a nested data structure in Python?
A nested structure is a list or dictionary that holds other lists or dictionaries inside it.
The simplest one is a list of lists. Think of a classroom seating plan with rows and seats.
grid[1] gives the whole second row. grid[1][2] then picks the third seat in that row.
▶seats = [["Aarav", "Diya", "Kabir"],2 ["Meera", "Ravi", "Sana"]]3row = seats[1]4print("Second row:", row)5print("Row 2, seat 3:", seats[1][2])6seats[0][1] = "Isha"7print(seats[0])
No variables yet.
▌✏️ Change this code and run it yourself
Second row: ['Meera', 'Ravi', 'Sana'] Row 2, seat 3: Sana ['Aarav', 'Isha', 'Kabir']
💡 Read the index numbers from left to right: first the row, then the item inside that row.
How do I loop through a list of lists?
Use one loop for the outer list and another loop inside it for each inner list.
Here each inner list holds one student's marks in three tests. We add up each row.
The inner loop finishes a whole row before the outer loop moves to the next row.
▶tests = [[70, 80, 90],2 [55, 65, 60]]3for row in tests:4 total = 05 for mark in row:6 total += mark7 print(row, "total:", total)
No variables yet.
▌✏️ Change this code and run it yourself
[70, 80, 90] total: 240 [55, 65, 60] total: 180
💡 You could also write sum(row). The double loop shows how Python visits every single item.
How do I store records as a list of dictionaries?
A dictionary is perfect for one record, like one student with a name and marks.
Put many such dictionaries in a list, and you have a small table, like a spreadsheet.
students[0] is the first dictionary. students[0]["name"] is the name inside it.
1students = [▶ {"name": "Riya", "marks": 88},3 {"name": "Arjun", "marks": 74},4]5print(students[0]["name"])6for s in students:7 print(f"{s['name']} scored {s['marks']}")
No variables yet.
▌✏️ Change this code and run it yourself
Riya Riya scored 88 Arjun scored 74
💡 Inside an f-string with double quotes, write the key with single quotes, like s['name'].
How do I put a list inside a dictionary?
Sometimes one key needs many values. A school timetable has many subjects on each day.
Store a list as the value. Then timetable["Mon"] is a whole list you can loop over or append to.
This is a dictionary of lists. It is very common in real programs.
First period on Monday: Maths Tuesday: ['English', 'Maths', 'Art'] Periods on Monday: 3
How do I change and add data deep inside a nested structure?
First reach the inner item with indexes and keys. Then assign a new value, as usual.
To add a new record, append() a whole new dictionary to the list.
A list comprehension can then pull out just the parts you need, like all the names.
['Riya', 'Arjun', 'Zoya'] [88, 79, 92] 80 and above: ['Riya', 'Zoya']
What is a dictionary of dictionaries?
When each record has a unique name, you can use that name as the key.
The value is another dictionary with the details. Then cities["Pune"]["state"] reads like a sentence.
Loop with items() to get each key and its inner dictionary together.
Maharashtra Pune: 31 degrees in Maharashtra Shimla: 18 degrees in Himachal Pradesh
💡 Choose the shape that fits the question you ask most. Look up by name? Use a dictionary of dictionaries.
🤔 Predict the output
Think like Python: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
t = [[1, 2], [3, 4], [5, 6]] print(t[2][0])
Question 2. What does this print?
s = {"name": "Anu", "marks": [70, 85, 90]}
print(s["marks"][-1] - s["marks"][0])Question 3. What does this print?
team = [{"n": "Ravi", "runs": 30}, {"n": "Isha", "runs": 45}]
total = 0
for p in team:
total += p["runs"]
print(len(team), total)🧩 Your challenge
A dictionary stores each student's marks in three tests. For every student, print the total and the average with one decimal place, using round(). Then print the name of the student with the highest total.
Aarav: total 253, average 84.3 Diya: total 275, average 91.7 Kabir: total 190, average 63.3 Topper: Diya
⚠️ Common mistakes (and how to fix them)
❌ Wrong
students = [{"name": "Riya"}, {"name": "Arjun"}]
print(students["name"])TypeError: list indices must be integers or slices, not str
✅ Right
students = [{"name": "Riya"}, {"name": "Arjun"}]
print(students[0]["name"])students is a list, so it needs a number first. Pick a dictionary with [0], then use the key.
❌ Wrong
city = {"name": "Pune", "temp": 31}
print(city["Temp"])KeyError: 'Temp'
✅ Right
city = {"name": "Pune", "temp": 31}
print(city["temp"])Keys must match exactly, including small and capital letters. "Temp" and "temp" are different keys.
❌ Wrong
row = [0, 0] grid = [row, row] grid[0][0] = 5 print(grid)
No error message – but it prints [[5, 0], [5, 0]]. Both rows changed!
✅ Right
grid = [[0, 0], [0, 0]] grid[0][0] = 5 print(grid)
Both rows are the same list, stored twice. Make separate inner lists so each row can change on its own.
📌 Remember
- Nested data means lists or dictionaries stored inside other lists or dictionaries.
- A list of lists uses two indexes: grid[row][item].
- A list of dictionaries is like a table, with one dictionary for each record.
- A dictionary of lists stores many values under one key, like a timetable.
- Reach the inner item first, then read it, change it or append to it.
- Choose the shape that makes your most common question easy to answer.
Finished the quiz and the challenge?
Questions students ask
How deep can nested data go in Python?
There is no small limit. But two or three levels are usually enough, and deeper data gets hard to read.
Should I use a list of dictionaries or a dictionary of dictionaries?
Use a list when order matters or names repeat. Use a dictionary when you look records up by a unique name.
Why did changing one row change another row too?
Both rows were the very same list. Python stored one list twice, so a change shows in both places.
Is nested data like JSON?
Yes! JSON data from websites and apps looks just like Python lists and dictionaries nested together.