10) Conditionals and Loops Lesson

Python Nested For Loop

5 min to complete · By Martin Breuss

The for loops are useful beyond doing some action to each character in a string or number in a range. You'll keep encountering them, and you'll learn more about how they can be effective tools for your automation scripts.

Multiple For Loops

Sometimes, it might not be enough to use a single for loop to do what you want to do. It's possible to nest loops within each other and create complex iterations.

Colorful illustration of a light bulb

Info: Generally, you should avoid nesting more than two to a maximum of three loops within each other.

How to Create a Nested For Loop

Creating nested loops can get memory intensive when you're working with large collections, and they can also quickly get difficult to understand. Still, there are situations where you'll need to use nested loops. Take a look at this example:

for i in range(5):
    for char in "hello":
        print(i, char)

This nested for loop prints out each character of the word hello five times. To make it easier to see what's going on, you're also printing the numerical iterator i alongside the characters:

0 h
0 e
0 l
0 l
0 o
1 h
1 e
1 l
1 l
1 o
2 h
2 e
2 l
2 l
2 o
3 h
3 e
3 l
3 l
3 o
4 h
4 e
4 l
4 l
4 o

That's a lot of lines, numbers, and characters! The numbers on the left side show you which iteration of the outer loop you're in, and the changing characters show you the iterations of the inner for loop. And just like that, you get rewarded with four times a happy, vertical hello!

Indentation

One thing to stay alert about when writing nested for loops is that you need to be consistent with your indentation. Each loop body needs to be indented four spaces. Since the second loop is in the body of the first loop, you end up indenting twice, meaning eight spaces, for the body of your second loop.

When to Use Nested For Loops

You might not need to use nested for loops in your scripts much. Still, it's good to be aware that this is possible, and you might run into some useful applications for it down the road. However, keep in mind that nested loops can make your code execution significantly slower. When you're working with large collections, there are usually better ways to tackle such challenges.

Tasks

  • Pick apart the nested loop as you picked apart the for loop in the previous lesson.
  • Write down what each line of code does in your notebook. Make a sketch of what's happening in the code.

For now, this is enough about for loops and definite iteration in Python. The language also has a second type of loop, the indefinite iteration, which uses the while keyword. You'll learn when you would choose a while loop over a for loop in the next section. In the upcoming section, you'll also collect and work with user input on the command line.

Summary: Python Nested For Loop

  • Nested for loops is when a for loop exists inside of another one
  • Nested for loops can make your code harder to understand and increase computational power
  • Indentation is important when working with nested for loops to ensure that each code is in the right loop