Learn Without Walls
← Previous Lesson Lesson 4 of 4 Practice Problems →

Lesson 5.4: Nested Loops

What You'll Learn

What Are Nested Loops?

A nested loop is a loop inside another loop. The inner loop runs completely for each iteration of the outer loop.

Nested loop: A loop placed inside the body of another loop. The inner loop completes all its iterations before the outer loop advances to its next iteration.
for i in range(3):         # Outer loop: runs 3 times
    for j in range(2):     # Inner loop: runs 2 times PER outer iteration
        print(f"i={i}, j={j}")
i=0, j=0 i=0, j=1 i=1, j=0 i=1, j=1 i=2, j=0 i=2, j=1

The inner loop ran 2 times for each of the 3 outer iterations, giving us 3 × 2 = 6 total prints. This multiplication is key to understanding nested loops.

Tracing Nested Loops

To understand nested loops, trace through them step by step. Write down the values of each variable at each step.

# Trace this code carefully
for row in range(1, 4):
    for col in range(1, 4):
        print(f"{row}x{col}={row*col}", end="  ")
    print()  # New line after each row
1x1=1 1x2=2 1x3=3 2x1=2 2x2=4 2x3=6 3x1=3 3x2=6 3x3=9
Tracing tip: The print() at the end of the outer loop (but outside the inner loop) creates a new line after each row. This is the standard pattern for printing grids.

Printing Patterns

Nested loops are commonly used to create text-based patterns.

Pattern: Right Triangle

for i in range(1, 6):
    for j in range(i):
        print("* ", end="")
    print()
* * * * * * * * * * * * * * *

Pattern: Rectangle

rows = 4
cols = 6

for i in range(rows):
    for j in range(cols):
        print("* ", end="")
    print()
* * * * * * * * * * * * * * * * * * * * * * * *

Pattern: Number Triangle

for i in range(1, 6):
    for j in range(1, i + 1):
        print(j, end=" ")
    print()
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

Multiplication Table

A classic nested loop example: generating a full multiplication table.

# Multiplication table (1-5)
print("    ", end="")
for i in range(1, 6):
    print(f"{i:4}", end="")
print()
print("    " + "----" * 5)

for i in range(1, 6):
    print(f"{i:2} |", end="")
    for j in range(1, 6):
        print(f"{i*j:4}", end="")
    print()
1 2 3 4 5 -------------------- 1 | 1 2 3 4 5 2 | 2 4 6 8 10 3 | 3 6 9 12 15 4 | 4 8 12 16 20 5 | 5 10 15 20 25

Working with 2D Data

Nested loops are essential when working with data organized in rows and columns, like a grid or a table.

Example: Processing a Grid of Scores

# Student scores: each inner list is one student's scores
all_scores = [
    [85, 90, 78],   # Student 1
    [92, 88, 95],   # Student 2
    [76, 82, 89],   # Student 3
]

for i in range(len(all_scores)):
    total = 0
    for score in all_scores[i]:
        total += score
    avg = total / len(all_scores[i])
    print(f"Student {i+1} average: {avg:.1f}")
Student 1 average: 84.3 Student 2 average: 91.7 Student 3 average: 82.3

Example: Finding a Value in a Grid

grid = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]

target = 5
found = False

for row in range(len(grid)):
    for col in range(len(grid[row])):
        if grid[row][col] == target:
            print(f"Found {target} at row {row}, col {col}")
            found = True

if not found:
    print(f"{target} not found")
Found 5 at row 1, col 1

Performance Considerations

Nested loops multiply the number of operations. A loop that runs 100 times inside a loop that runs 100 times means 10,000 total iterations. Be aware of this when working with large data.

# Counting total iterations
count = 0

for i in range(10):
    for j in range(10):
        count += 1

print(f"Total iterations: {count}")  # 100
Total iterations: 100
Rule of thumb: If the outer loop runs n times and the inner loop runs m times, the total number of inner-loop iterations is n × m. Three levels of nesting with 10 iterations each gives 1,000 total iterations.

Try It Yourself!

Create a pattern that looks like this (pyramid with 5 rows):

    *
   ***
  *****
 *******
*********

Hint: For each row, print spaces first, then stars. Row i has (n-i-1) spaces and (2*i+1) stars.

Check Your Understanding

How many times will "X" be printed?

for i in range(4):
    for j in range(3):
        print("X")

Answer: 12 times

Outer loop runs 4 times (i = 0, 1, 2, 3). For each outer iteration, the inner loop runs 3 times (j = 0, 1, 2). Total: 4 × 3 = 12.

Key Takeaways

← break, continue, pass Lesson 4 of 4 Practice Problems →