[Memo] Several Ideas for Interrupting Multiple Loops in Python

By 苏剑林 | December 19, 2016

Breaking Out of a Single Loop

Regardless of the programming language, there is often a need to break out of a loop—for example, when enumerating values and finding a number that satisfies a condition, you want to terminate. Breaking out of a single loop is very simple, for instance:

for i in range(10):
    if i > 5:
        print i
        break

However, we sometimes need to break out of multiple loops, while break can only exit one level of a loop. For example:

for i in range(10):
    for j in range(10):
        if i+j > 5:
            print i,j
            break

This code does not stop as soon as it finds a single set where $i+j > 5$; instead, it will find 10 such sets, because the break only exits the for j in range(10) loop level. So, how can we break out of multiple loops? This post serves as a memo for those methods.

Breaking Out of Multiple Loops

In fact, Python's standard syntax does not support breaking out of multiple loops directly. Therefore, one must rely on certain techniques. The general ideas include: wrapping the logic into a function, utilizing Cartesian products, or using a debugging/exception-based approach.

Wrapping in a Function

In Python, a function stops executing as soon as it reaches a return statement. One can take advantage of this characteristic by wrapping the functionality into a function to terminate multiple loops. For example:

def work():
    for i in range(10):
        for j in range(10):
            if i+j > 5:
                return i,j

print work()

Utilizing Cartesian Product

The idea behind this method is that since we can break out of a single loop, we can rewrite multiple loops as a single loop. This can be achieved using the product function from itertools to create a Cartesian product. For example:

from itertools import product

for i,j in product(range(10), range(10)):
    if i+j > 5:
        print i,j
        break

Utilizing Debugging Mode

The Cartesian product method is clever and concise, but it can only be used in cases where the collection in each loop iteration is independent. If each layer of the loop is closely related to the previous layer, this technique cannot be used. In such cases, you can use the first method (wrapping it in a function), or alternatively, utilize a debugging-style approach. This leverages the principle that an execution will exit whenever an error occurs, by "disguising" a custom exception as the trigger.

class Found(Exception):
    pass

try:
    for i in range(10):
        for j in range(i): # The second loop is dependent on the first
            if i + j > 5:
                raise Found
except Found:
    print i, j

If you found this article helpful, you are welcome to share or tip this article. Tipping is not about making a profit, but is a way to see how much genuine attention Scientific Space has received from its readers. Of course, if you ignore it, it will not affect your reading. Thank you again for visiting!

To cite this article, please refer to:

You might also be interested in the following content: