Inner loop and outer loop in python

This article describes how to break out of nested loops in Python.

  • How to write nested loops in Python
  • Break out of nested loops with else and continue
  • Break out of nested loops with a flag variable
  • Avoid nested loops with itertools.product()
  • Speed comparison

See the following article for the basic usage of the for loop in Python.

  • for loop in Python (with range, enumerate, zip, etc.)

How to write nested loops in Python

In Python, nested loops (multiple loops) are written as follows. Blocks are represented by indents in Python, so just add more indents.

l1 = [1, 2, 3]
l2 = [10, 20, 30]

for i in l1:
    for j in l2:
        print(i, j)
# 1 10
# 1 20
# 1 30
# 2 10
# 2 20
# 2 30
# 3 10
# 3 20
# 3 30

When break is executed in the inner loop, it only exits from the inner loop and the outer loop continues.

for i in l1:
    for j in l2:
        print(i, j)
        if i == 2 and j == 20 :
            print('BREAK')
            break
# 1 10
# 1 20
# 1 30
# 2 10
# 2 20
# BREAK
# 3 10
# 3 20
# 3 30

Break out of nested loops with else and continue

In Python's for loop, you can use else and continue in addition to break.

  • for loop in Python (with range, enumerate, zip, etc.)

You can break all loops with else and continue.

for i in l1:
    for j in l2:
        print(i, j)
        if i == 2 and j == 20:
            print('BREAK')
            break
    else:
        continue
    break
# 1 10
# 1 20
# 1 30
# 2 10
# 2 20
# BREAK

The code with explanation is as follows.

for i in l1:
    print('Start outer loop')

    for j in l2:
        print('--', i, j)
        if i == 2 and j == 20:
            print('-- BREAK inner loop')
            break
    else:
        print('-- Finish inner loop without BREAK')
        continue

    print('BREAK outer loop')
    break
# Start outer loop
# -- 1 10
# -- 1 20
# -- 1 30
# -- Finish inner loop without BREAK
# Start outer loop
# -- 2 10
# -- 2 20
# -- BREAK inner loop
# BREAK outer loop

When the inner loop ends normally without break, continue in the else clause is executed. This continue is for the outer loop, and skips break in the outer loop and continues to the next cycle.

When the inner loop ends with break, continue in the else clause is not executed. In this case, break in the outer loop is executed.

As a result, whenever the inner loop ends with break, break in the outer loop is also executed.

The idea is the same even if the number of loops increases. An example of a triple loop is as follows.

l1 = [1, 2, 3]
l2 = [10, 20, 30]
l3 = [100, 200, 300]

for i in l1:
    for j in l2:
        for k in l3:
            print(i, j, k)
            if i == 2 and j == 20 and k == 200:
                print('BREAK')
                break
        else:
            continue
        break
    else:
        continue
    break
# 1 10 100
# 1 10 200
# 1 10 300
# 1 20 100
# 1 20 200
# 1 20 300
# 1 30 100
# 1 30 200
# 1 30 300
# 2 10 100
# 2 10 200
# 2 10 300
# 2 20 100
# 2 20 200
# BREAK

Break out of nested loops with a flag variable

The above way of using else and continue may be difficult to understand for those unfamiliar with Python.

Adding a flag variable may make the code easier for many to understand.

In the condition that the inner loop ends with break, set the flag to True, and in the outer loop, set break according to the flag.

Double-loop:

l1 = [1, 2, 3]
l2 = [10, 20, 30]

flag = False
for i in l1:
    for j in l2:
        print(i, j)
        if i == 2 and j == 20:
            flag = True
            print('BREAK')
            break
    if flag:
        break
# 1 10
# 1 20
# 1 30
# 2 10
# 2 20
# BREAK

Triple-loop:

l1 = [1, 2, 3]
l2 = [10, 20, 30]
l3 = [100, 200, 300]

flag = False
for i in l1:
    for j in l2:
        for k in l3:
            print(i, j, k)
            if i == 2 and j == 20 and k == 200:
                flag = True
                print('BREAK')
                break
        if flag:
            break
    if flag:
        break
# 1 10 100
# 1 10 200
# 1 10 300
# 1 20 100
# 1 20 200
# 1 20 300
# 1 30 100
# 1 30 200
# 1 30 300
# 2 10 100
# 2 10 200
# 2 10 300
# 2 20 100
# 2 20 200
# BREAK

You can avoid nested loops with itertools.product().

  • Cartesian product of lists in Python (itertools.product)

You can use itertools.product() to get all combinations of multiple lists in one loop and get the same result as nested loops.

import itertools

l1 = [1, 2, 3]
l2 = [10, 20, 30]

for i, j in itertools.product(l1, l2):
    print(i, j)
# 1 10
# 1 20
# 1 30
# 2 10
# 2 20
# 2 30
# 3 10
# 3 20
# 3 30

Since it is a single loop, you can simply break under the desired conditions.

for i, j in itertools.product(l1, l2):
    print(i, j)
    if i == 2 and j == 20:
        print('BREAK')
        break
# 1 10
# 1 20
# 1 30
# 2 10
# 2 20
# BREAK

Adding the argument of itertools.product(), you can execute the process corresponding to more multiple loops.

l1 = [1, 2, 3]
l2 = [10, 20, 30]
l3 = [100, 200, 300]

for i, j, k in itertools.product(l1, l2, l3):
    print(i, j, k)
    if i == 2 and j == 20 and k == 200:
        print('BREAK')
        break
# 1 10 100
# 1 10 200
# 1 10 300
# 1 20 100
# 1 20 200
# 1 20 300
# 1 30 100
# 1 30 200
# 1 30 300
# 2 10 100
# 2 10 200
# 2 10 300
# 2 20 100
# 2 20 200
# BREAK

Note

In itertools.product(), the process for the element is always executed for all combinations.

In the following example, the multiplication is performed 9 times for both i and j.

for i, j in itertools.product(l1, l2):
    x = i * 2 + j * 3
    print(i, j, x)
# 1 10 32
# 1 20 62
# 1 30 92
# 2 10 34
# 2 20 64
# 2 30 94
# 3 10 36
# 3 20 66
# 3 30 96

In the case of nested loops, the process for the outer loop is executed by the number of outer elements.

In the following example, the multiplication for the variable i is only 3 times.

for i in l1:
    temp = i * 2
    for j in l2:
        x = temp + j * 3
        print(i, j, x)
# 1 10 32
# 1 20 62
# 1 30 92
# 2 10 34
# 2 20 64
# 2 30 94
# 3 10 36
# 3 20 66
# 3 30 96

Speed comparison

The result of measuring the execution time of each way with the magic command %%timeit of Jupyter Notebook is shown. Note that it cannot be measured if executed as Python code.

  • Measure execution time with timeit in Python

Please note that the results will differ depending on the number of elements and the number of for loops to be nested.

Take a triple loop with 100 elements as an example.

import itertools

n = 100
l1 = range(n)
l2 = range(n)
l3 = range(n)

x = n - 1

%%timeit
for i in l1:
    for j in l2:
        for k in l3:
            if i == x and j == x and k == x:
                break
        else:
            continue
        break
    else:
        continue
    break
# 43 ms ± 1.33 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

%%timeit
flag = False
for i in l1:
    for j in l2:
        for k in l3:
            if i == x and j == x and k == x:
                flag = True
                break
        if flag:
            break
    if flag:
        break
# 45.2 ms ± 3.42 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

%%timeit
for i, j, k in itertools.product(l1, l2, l3):
    if i == x and j == x and k == x:
        break
# 55.8 ms ± 458 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

Using else, continue and adding flag variables are roughly equivalent, and itertools.product() is slow.

However, in some cases, itertools.product() is more suitable because it improves the readability of code even if it is slow. You should use it depending on the situation.

What are the two types of loops in Python?

Answer: Python generally supports two types of loops: for loop and while loop. However, a third loop[nested loop] can be generated by nesting two or more of these loops.

What are the 3 types of loops in Python?

Loop Types.
while loop..
for loop..
nested loops..

Which is the inner loop?

In computer programs, an important form of control flow is the loop which causes a block of code to be executed more than once. A common idiom is to have a loop nested inside another loop, with the contained loop being commonly referred to as the inner loop.

How do you break inner and outer loops in Python?

5 Ways To Break Out of Nested Loops in Python. Not as elegant as it should be. ... .
Add a Flag Variable. This is an effective solution. ... .
Raise an Exception. If we can't use the break keyword as expected. ... .
Check the Same Condition Again. ... .
Use the For-Else Syntax. ... .
Put It Into a Function..