The dragon explains
Loops inside loops
The dragon: So far every loop you've written stands alone. Now let's put one loop INSIDE another.
The dragon: This checks every cell of a 2x2 grid — row by row, and within each row, column by column.
example.pyPython 3for row in range(2): for col in range(2): print(row, col)
Output0 0 0 1 1 0 1 1The dragon: Look at that order closely. row stays 0 for TWO full lines — col runs 0, then 1 — before row ever becomes 1.
example.pyPython 3for row in range(2): for col in range(2): print(row, col)
The dragon: That's the whole trick: the INNER loop (col) runs all the way to the end, every single time, before the OUTER loop (row) moves on even once. They never alternate.
The dragon: This works the same way to read real grid contents — grid[row][col] picks row first, then the item at that column inside it.
example.pyPython 3grid = [["A", "B"], ["C", "D"]]for row in range(2): for col in range(2): print(grid[row][col])
OutputA B C DThe dragon: Careful with which variable goes where. grid[row][row] would use row twice by accident — col has to be the one that changes the fastest, inside the brackets on the right.
example.pyPython 3grid = [["A", "B"], ["C", "D"]]for row in range(2): for col in range(2): print(grid[row][col])
The dragon: One more thing worth counting: how many times does that innermost print() actually run?
example.pyPython 3for row in range(3): for col in range(2): print(row, col)
Output0 0 0 1 1 0 1 1 2 0 2 1The dragon: 3 outer passes, 2 inner passes each — that's 3 times 2, not 3 plus 2. Six lines. Nested loops multiply, they don't just add.
Quick check
Check your understanding
Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.
Question 1 of 3
for row in range(2): for col in range(2): print(row, col) — what's the FIRST value row becomes AFTER col has printed both 0 and 1?
question.pyPython 3for row in range(2): for col in range(2): print(row, col)
- 1 — row only changes once col has finished ALL its values for the current rowCorrect
- col and row take turns, one step each — so row becomes 1 right after row 0, col 0
- row never changes — only the innermost variable actually moves
Why: The inner loop finishes ENTIRELY — every value col can take — before the outer loop moves to its next value. That's why the output is 0 0, 0 1, then 1 0, 1 1: two full inner passes under row=0, then two more under row=1. Nothing alternates.
Question 2 of 3
grid = [["A", "B"], ["C", "D"]]. Inside the nested loop, a line reads print(grid[row][row]). What actually prints?
question.pyPython 3grid = [["A", "B"], ["C", "D"]]for row in range(2): for col in range(2): print(grid[row][row])
- A, A, D, D — because row is used in both bracket positions, col is never actually usedCorrect
- A, B, C, D — the same as if col had been used correctly
- This crashes, because col is never used anywhere
Why: grid[row][row] locks the second position to whatever row is, so col changing does nothing to this line's result. row=0: grid[0][0] twice -> A, A. row=1: grid[1][1] twice -> D, D. The fix is grid[row][col], so the SECOND position actually follows the inner loop.
Question 3 of 3
An outer loop runs 4 times. Its inner loop runs 3 times on EACH of those passes. How many times does the innermost print() run in total?
- 12 — 4 times 3Correct
- 7 — 4 plus 3
- 4 — only the outer loop's count matters
Why: Every one of the outer loop's 4 passes triggers a COMPLETE run of the inner loop's 3 passes — 4 groups of 3, which is 12, not 4+3. This is why nested loops can produce output much longer than either number alone would suggest.
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
GuidedYour first try
This code visits every cell of a 2x2 grid using a loop inside a loop. Before you change anything, just RUN it and watch the order the coordinates come out in.
- +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 PROStuck three times? The game offers the answer — unasked.