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 4
  • PRO
  • Ages 10+
  • 3 hands-on levels
See what PRO unlocksA one-time $49.99 purchase. Topics 1–3 are free.

The dragon explains

Nested access and the two-level loop

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

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

    example.pyPython 3
    party = [{'name': 'Ember', 'hp': 40}, {'name': 'Blaze', 'hp': 25}]for dragon in party:    print(dragon['name'], 'has', dragon['hp'], 'hp')
    OutputEmber has 40 hp Blaze has 25 hp
  3. The dragon: A dict of lists needs a LOOP INSIDE A LOOP — one loop for the categories, another for each category's items.

    example.pyPython 3
    inventory = {'weapons': ['sword', 'bow'], 'potions': ['health']}for category in inventory:    for item in inventory[category]:        print(category, ':', item)
    Outputweapons : sword weapons : bow potions : health
  4. The dragon: Now updating a nested value — a genuinely new move, not just a deeper read.

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

    example.pyPython 3
    party = [{'name': 'Ember', 'hp': 40}, {'name': 'Blaze', 'hp': 25}]party[0]['hp'] = party[0]['hp'] - 10print(party[0]['hp'])
    Output30
  6. The dragon: Same read-old-value-first idea as any variable update — just applied one level deeper, on both sides of the =.

Quick check

Check your understanding

Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.

  1. Question 1 of 2

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

    question.pyPython 3
    for category in inventory:    print(category)
    • Just the KEYS — 'weapons' and 'potions' — not the items inside each listCorrect
    • 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]:.

  2. Question 2 of 2

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

    question.pyPython 3
    party[0]['hp'] = party[0]['hp'] - 10
    • The current hp value, BEFORE this line changes it — same as any read-old-value-first updateCorrect
    • 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'].

In the game

What you'll practice

Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.

  1. 1

    GuidedYour first try

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

  2. +2

    PracticeTwo more, on your own

This lesson is part of PRO.

PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.

Unlock this lesson with PRO

Stuck three times? The game offers the answer — unasked.