In Post #29, we saw how placing an if
statement inside another one—a technique called nesting—allowed us to create more detailed, multi-level logic. We can apply the exact same principle to loops to solve more complex problems.
In this post, we will explore the concept of nested loops. You’ll learn how a loop running inside another loop behaves and see why this is a powerful technique for working with multi-dimensional or related sets of data.
The Concept: An Outer and an Inner Loop
A nested loop is simply a loop that exists inside the body of another loop. The first loop is called the outer loop, and the loop inside it is called the inner loop.
The fundamental rule to understand is this: for every one iteration of the outer loop, the inner loop will run through all of its iterations completely.
A great analogy is a clock. The hour hand is the outer loop, and the minute hand is the inner loop. For the hour hand to move just once (from 1 to 2), the minute hand must complete its entire cycle of 60 movements.
A Simple Demonstration
The best way to see the flow of execution is with a simple example that prints the state of each loop.
# The outer loop
for i in range(3): # This loop will run 3 times (i will be 0, 1, 2)
print(f"--- Outer loop iteration: {i} ---")
# The inner loop
for j in range(2): # This loop will run 2 times for each outer loop (j will be 0, 1)
print(f" Inner loop iteration: {j}")
Let’s trace the output of this code step-by-step:
--- Outer loop iteration: 0 ---
Inner loop iteration: 0
Inner loop iteration: 1
--- Outer loop iteration: 1 ---
Inner loop iteration: 0
Inner loop iteration: 1
--- Outer loop iteration: 2 ---
Inner loop iteration: 0
Inner loop iteration: 1
As you can see, the outer loop begins its first iteration (where i
is 0
), prints its message, and then starts the inner loop. The inner loop runs to completion (for j=0
and j=1
). Only after the inner loop is finished does the outer loop begin its second iteration (where i
is 1
), which triggers the inner loop to run to completion all over again.
A Practical Use Case: Generating Combinations
Nested loops are perfect for situations where you need to combine every item from one collection with every item from another. For example, let’s say we have a list of T-shirt colors and a list of sizes, and we want to list all possible products we can offer.
colors = ["Red", "Blue"]
sizes = ["S", "M", "L"]
print("All possible T-shirt products:")
for color in colors:
for size in sizes:
print(f"- {color} T-shirt, size {size}")
The output will be every possible pairing:
All possible T-shirt products:
- Red T-shirt, size S
- Red T-shirt, size M
- Red T-shirt, size L
- Blue T-shirt, size S
- Blue T-shirt, size M
- Blue T-shirt, size L
For the first outer loop (color = "Red"
), the inner loop runs three times, pairing “Red” with “S”, “M”, and “L”. Then, for the second outer loop (color = "Blue"
), the inner loop runs another three times.
What’s Next?
Nested loops are a powerful tool for handling more complex repetitive tasks, especially when dealing with combinations of data or grid-like structures. The key is to remember that the inner loop always completes its full run for each single step of the outer loop.
Understanding the flow of nested loops is one thing, but seeing them create something visual can make the concept much clearer. In Post #42, we will have some fun with nested loops by using them to print patterns like squares and triangles to the console.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com