Understanding Nested Loops in Python | Lesson 9

Nested loops involve placing one loop inside another; the inner loop is executed every time the outer loop is repeated. This structure is useful for handling data on a multidimensional level.

Grammarly Writing Support

In this lesson you will learn

  • How nested Loops work;
  • How to read and understand a nested for loop; and
  • The intricate workings of the nested while loop.

If you are new to loops in Python, you should read about while and for loops in Python first.

How nested loops in Python work

The execution flow of nested loops is ordered and systematic.

  1. The outer loop will begin the first cycle.
  2. The execution then moves to the inner loop, which will go from the beginning to the end, repeating as many times as programmed.
  3. Once the inner loop completes its cycle, it then switches to the outer loop once again to begin its second cycle.
  4. The inner loop is once again activated to complete its second cycle.
  5. This is a repeatable process until the outer loop completes all of its cycles as programmed.

Nested for loop | Nested loops

Here is a visual example of a simple nested for loop

for a in range(4):
  print(f"outer loop iteration: {a}")
  for b in range(8):
    print(f"inner loop iteration: {b}")   

Now, this has a condition that isn’t as clear as the one in the usual for and while loops. Look at the line for a in range(4):. and for b in range(8). Range(4) means the outer loop runs 4 times (0, 1, 2, 3) while range(8) means that the inner loop will run 8 times(0, 1, 2, 3, 4, 5, 6, 7).

Each of these range() functions holds one argument, 4 and 8, respectively. The inner loop would execute once for every iteration of the outer loop.

When the code prints, this is the outcome that is expected:

outer loop iteration: 0

inner loop iteration: 0
inner loop iteration: 1
inner loop iteration: 2
inner loop iteration: 3
inner loop iteration: 4
inner loop iteration: 5
inner loop iteration: 6
inner loop iteration: 7

outer loop iteration: 1

inner loop iteration: 0
inner loop iteration: 1
inner loop iteration: 2
inner loop iteration: 3
inner loop iteration: 4
inner loop iteration: 5
inner loop iteration: 6
inner loop iteration: 7

outer loop iteration: 2

inner loop iteration: 0
inner loop iteration: 1
inner loop iteration: 2
inner loop iteration: 3
inner loop iteration: 4
inner loop iteration: 5
inner loop iteration: 6
inner loop iteration: 7

outer loop iteration: 3

inner loop iteration: 0
inner loop iteration: 1
inner loop iteration: 2
inner loop iteration: 3
inner loop iteration: 4
inner loop iteration: 5
inner loop iteration: 6
inner loop iteration: 7

What are nested while loops | Nested loops

A nested while loop is a while loop inside another while loop. For each iteration of the outer loop, the inner loop runs from beginning to end before the outer loop proceeds to its next iteration.

Simple example: Multiplication table

outer = 1
while outer <= 5: # outer loop rows
  inner =1
  while inner <=12:
    result = outer*inner
    print(f"{outer} * {inner} = {result}")
    inner+=1
  print("_" * 15)
  outer+=1

Explanation of the nested while loop

Here is a clear, line-by-line explanation of the above nested while loop program:

outer = 1

A variable named outer is created and assigned the value 1. This variable controls the outer loop.

while outer <= 5: # outer loop rows

This starts the outer while loop. The loop will continue running as long as outer is less than or equal to 5. This means the outer loop will run 5 times (for values 1 through 5).

inner = 1

Inside the outer loop, a variable named inner is created and set to 1. This resets inner every time the outer loop runs. Inner controls the inner loop.

while inner <= 12:

This starts the inner while loop. It will continue running as long as inner is less than or equal to 12. This means the inner loop runs 12 times for each outer loop iteration.

result = outer * inner

The program multiplies outer and inner. The product is stored in the variable result. This creates a multiplication table effect.

print(f”{outer} * {inner} = {result}”)

This prints the multiplication expression and its result.
This increases the value of inner by 1. Without this line, the inner loop would run forever (infinite loop). It ensures the inner loop eventually stops at 12.

print(“_” * 15)

After the inner loop finishes, this prints a line of 15 underscores which visually separates each set of multiplication results.

outer += 1

This increases the value of outer by 1. This allows the outer loop to move to the next number (2, then 3, etc.). Without this line, the outer loop would also become an infinite loop.

Output for the nested while loop

Below is the output for this nested while loop:

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9
1 * 10 = 10
1 * 11 = 11
1 * 12 = 12
_______________
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
2 * 11 = 22
2 * 12 = 24
_______________
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
3 * 11 = 33
3 * 12 = 36
_______________
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
4 * 11 = 44
4 * 12 = 48
_______________
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
5 * 11 = 55
5 * 12 = 60
_______________

It will continue from one through five until all five numbers are run, after which the code ends when it detects a false input.

What are the types of loops in Python?

There are two types of loops in Python. These are the for and while loops.

What are nested loops good for?

Nested loops are powerful when you need to work with multidimensional data such as grids, tables, or matrices. They allow you to repeat an action inside another repeated action making them ideal for handling rows and columns, generating patterns, or performing structured calculations like multiplication tables.

Okonkwo, C. W., & Ade-Ibijola, A. (2023). Synthesis of nested loop exercises for practice in introductory programming. International Journal of Emerging Technologies in Learning (iJET), 18(1).

Leave a Comment