PY-101 ยท Loops
Nested loops
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
A nested loop is a loop inside another loop: for every round of the outer loop, the inner loop runs all the way through. They are how you work with rows and columns, like tables, grids and game boards. If the outer loop runs 3 times and the inner 4, the inner block runs 12 times.
- Put a loop inside a loop
- Work out how many times code runs
- Build rows and columns
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
for row in "AB":line = ""for seat in range(1, 4):line = line + f"{row}{seat} "print(line.strip())
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
A1 A2 A3
B1 B2 B3 ๐ฏ Print a 3-row triangle of stars: row 1 has 1 star, row 2 has 2, row 3 has 3. Use nested loops (no string multiplication).
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
for r in range(1, rows + 1): for _ in range(r): print("*", end=""), then print()
- The problemA cinema has 5 rows of 8 seats. To label every seat A1 to E8 you go row by row, and inside each row, seat by seat.
- The Python idea
for row in rows:A nested loop is a loop inside another loop: for every round of the outer loop, the inner loop runs all the way through. - In real lifeSpreadsheet tools loop over every row, and inside each row over every column, to find empty cells.
Start with a question
A cinema has 5 rows of 8 seats. To label every seat A1 to E8 you go row by row, and inside each row, seat by seat.
Tables, images (rows of pixels), seating plans and every-pair comparisons all need a loop inside a loop.
Explain it like I'm 10
Think of a clock: the minute hand goes all the way round once for every single step of the hour hand.
The technical explanation
The inner loop restarts from the beginning on every outer round.
Use different names for the two loop variables, such as row and col.
Build each row inside the inner loop, then print it once the row is complete. Or use print(..., end="") to stay on one line and a plain print() to start the next row.
Syntax
for row in rows:
for col in cols:
... Real-world scenario
Spreadsheet tools loop over every row, and inside each row over every column, to find empty cells.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Defining functions โQuestions people ask
Are nested loops bad in Python?
No, they are the right tool for grids. They become slow when both loops are large, because the work multiplies.
How do I break out of two loops at once?
Put the loops in a function and return, or set a flag variable and check it in the outer loop.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner Are nested loops bad in Python?
No, they are the right tool for grids. They become slow when both loops are large, because the work multiplies.
beginner How do I break out of two loops at once?
Put the loops in a function and return, or set a flag variable and check it in the outer loop.
intermediate Outer loop runs 3 times, inner 5. How many times does the inner block run?
15. 3 ร 5 = 15.
intermediate When does the inner loop restart?
On every outer round. It runs fully for each outer round.
Quick revision
Remember
- Put a loop inside a loop
- Work out how many times code runs
- Build rows and columns
- Nested loops multiply work. 1,000 outer rounds times 1,000 inner rounds is a million. Keep inner loops small.
Syntax
for row in rows:
for col in cols:
... Official documentation
- More control flow tools (docs.python.org)