note.nkmk.me

Break out of nested loops in Python

Posted: 2020-08-26 / Tags: Python

There are several ways to break out of nested loops (multiple loops) in Python.

Contents:

  • How to write nested loops
  • Use else, continue
  • Add a flag variable
  • Avoid nested loops with itertools.product()
  • Speed comparison

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

Sponsored Link

How to write nested loops

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 still 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

Use else, continue

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

By using else and continue, you can get out of all the loops from inside.

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 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 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

Add a flag variable

The above way of using else and continue may be difficult to understand unless you are familiar with Python.

Adding a variable to use as a flag will probably 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
Sponsored Link

Avoid nested loops with itertools.product()

There is also a way to avoid nested loops by itertools.product().

You can use itertools.product() to get all combinations of multiple lists in one loop, and you can 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 the variable 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 even if it is executed as Python code.

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 late. You should use it depending on the situation.

Sponsored Link
Share

Related Categories

Related Posts