CurriculumChoosing Python Data Structures, for KidsNested access and the two-level loop

Looping Over Nested Data in Python: A Loop Inside a Loop

Looping over nested data, and updating a value that lives one level deep.

Lesson 2 of 4PROAges 10+
See what PRO unlocks →

Nested access and the two-level loop

Segment 1 read nested data with a direct index — party[0]['hp']. Real code usually loops over EVERY item instead.

for dragon in party: gives you one dragon's whole dict per pass — reach inside it the same way you always have.

party = [{'name': 'Ember', 'hp': 40}, {'name': 'Blaze', 'hp': 25}]
for dragon in party:
    print(dragon['name'], 'has', dragon['hp'], 'hp')

Output

Ember has 40 hp
Blaze has 25 hp

A dict of lists needs a LOOP INSIDE A LOOP — one loop for the categories, another for each category's items.

inventory = {'weapons': ['sword', 'bow'], 'potions': ['health']}
for category in inventory:
    for item in inventory[category]:
        print(category, ':', item)

Output

weapons : sword
weapons : bow
potions : health

Now updating a nested value — a genuinely new move, not just a deeper read.

party[0]['hp'] = party[0]['hp'] - 10 reads the OLD nested value, subtracts, and writes it back to the SAME nested spot.

party = [{'name': 'Ember', 'hp': 40}, {'name': 'Blaze', 'hp': 25}]
party[0]['hp'] = party[0]['hp'] - 10
print(party[0]['hp'])

Output

30

Same read-old-value-first idea as any variable update — just applied one level deeper, on both sides of the =.

Check your understanding

inventory = {'weapons': ['sword', 'bow'], 'potions': ['health']}. What does ONE for loop alone (just for category in inventory:) give you access to?

for category in inventory:
    print(category)
  • Just the KEYS — 'weapons' and 'potions' — not the items inside each list
  • Every individual item across all categories, flattened into one sequence

Why: Just the keys. for category in inventory: hands you 'weapons' then 'potions' — one string each pass. To reach the ITEMS inside each category's list, you need a second, nested loop: for item in inventory[category]:.

party[0]['hp'] = party[0]['hp'] - 10. What does the RIGHT side of the = read, exactly?

party[0]['hp'] = party[0]['hp'] - 10
  • The current hp value, BEFORE this line changes it — same as any read-old-value-first update
  • The NEW hp value, after this line has already run

Why: The current value, before this line changes anything. Python always works out the RIGHT side of an = first, using whatever value already existed — exactly like a flat variable's health = health - 10 — and only then writes the result back to party[0]['hp'].

What you'll practice

party = [{'name': 'Ember', 'hp': 40}, {'name': 'Blaze', 'hp': 25}]. Loop over every dragon and print its name and hp, labelled — one line per dragon.

Unlock this lesson with PRO →

More from this topic

Nested access and the two-level loop is one lesson inside Choosing Python Data Structures, for Kids — see the full lesson order and what the whole topic covers.

View the full topic →