My first instinct would be to refactor the nested loop into a function and use return to break out.

Answer from Robert Rossney on Stack Overflow
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-use-break-continue-and-pass-statements-when-working-with-loops-in-python-3
How to Break Out of Multiple Loops in Python | DigitalOcean
August 7, 2025 - Check out DigitalOcean App Platform and deploy a Python project directly from GitHub in minutes. ... The break statement in Python allows you to exit a loop immediately when a specific condition is met, which is especially useful for terminating early during search or validation operations.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ controlflow.html
4. More Control Flow Tools โ€” Python 3.14.3 documentation
The break statement breaks out of the innermost enclosing for or while loop: >>> for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print(f"{n} equals {x} * {n//x}") ... break ...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ break-continue
Python break and continue (With Examples)
We can also terminate the while loop using the break statement. For example, i = 0 while i < 5: if i == 3: break print(i) i += 1 ... The continue statement skips the current iteration of the loop and the control flow of the program goes to the next iteration.
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Breaking/continuing out of multiple loops - Ideas - Discussions on Python.org
August 21, 2022 - Hi! SUGGESTION An easy way to break/continue within nested loops. MY REAL LIFE EXAMPLE I am looping through a list of basketball players, each of which has multiple tables, each of which can have up to three different versions. I manipulate data in the deepest loop and, if some key data is ...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_loop_control.htm
Python Break, Continue and Pass Statements
for letter in 'Python': # First ... value : 7 Current variable value : 6 Good bye! The continue statement in Python returns the control to the beginning of the while loop....
Top answer
1 of 9
246
for ii in range(200):
    for jj in range(200, 400):
        ...block0...
        if something:
            break
    else:
        ...block1...

Break will break the inner loop, and block1 won't be executed (it will run only if the inner loop is exited normally).

2 of 9
130
for i in ...:
    for j in ...:
        for k in ...:
            if something:
                # continue loop i

In a general case, when you have multiple levels of looping and break does not work for you (because you want to continue one of the upper loops, not the one right above the current one), you can do one of the following

Refactor the loops you want to escape from into a function

def inner():
    for j in ...:
        for k in ...:
            if something:
                return


for i in ...:
    inner()

The disadvantage is that you may need to pass to that new function some variables, which were previously in scope. You can either just pass them as parameters, make them instance variables on an object (create a new object just for this function, if it makes sense), or global variables, singletons, whatever (ehm, ehm).

Or you can define inner as a nested function and let it just capture what it needs (may be slower?)

for i in ...:
    def inner():
        for j in ...:
            for k in ...:
                if something:
                    return
    inner()

Use exceptions

Philosophically, this is what exceptions are for, breaking the program flow through the structured programming building blocks (if, for, while) when necessary.

The advantage is that you don't have to break the single piece of code into multiple parts. This is good if it is some kind of computation that you are designing while writing it in Python. Introducing abstractions at this early point may slow you down.

Bad thing with this approach is that interpreter/compiler authors usually assume that exceptions are exceptional and optimize for them accordingly.

class ContinueI(Exception):
    pass


continue_i = ContinueI()

for i in ...:
    try:
        for j in ...:
            for k in ...:
                if something:
                    raise continue_i
    except ContinueI:
        continue

Create a special exception class for this, so that you don't risk accidentally silencing some other exception.

Something else entirely

I am sure there are still other solutions.

๐ŸŒ
Accuweb
accuweb.cloud โ€บ home โ€บ python break, continue, pass statements with examples
Python Break, Continue, Pass Statements with Examples
December 11, 2023 - A) Pass allows developers to define empty classes or functions during development without causing syntax errors. ... A) Yes. A loop can contain multiple break statements, but excessive use may reduce code readability.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ break-continue-and-pass-in-python
Loop Control Statements - Python
July 12, 2025 - ... # Using For Loop for i in range(5): ... += 1 ... Python Continue statement is a loop control statement that forces to execute the next iteration of the loop while skipping the rest of the code inside the loop for the current iteration only, ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-break-and-python-continue-how-to-skip-to-the-next-function
Python Break and Python Continue โ€“ How to Skip to the Next Function
March 14, 2022 - As you can see, the number 50 is not printed to the console because of the continue statement inside the if statement. The break and continue statements in Python are used to skip parts of the current loop or break out of the loop completely.
๐ŸŒ
Learn Python
learnpython.dev โ€บ 02-introduction-to-python โ€บ 110-control-statements-looping โ€บ 40-break-continue
break, continue, and return :: Learn Python by Nina Zakharenko
break and continue allow you to control the flow of your loops. Theyโ€™re a concept that beginners to Python tend to misunderstand, so pay careful attention. Using break The break statement will completely break out of the current loop, meaning it wonโ€™t run any more of the statements contained ...
๐ŸŒ
Python
peps.python.org โ€บ pep-3136
PEP 3136 โ€“ Labeled break and continue | peps.python.org
June 30, 2007 - The syntax of break and continue would be altered to allow multiple break and continue statements on the same line. Thus, break break would break out of the first and second enclosing loops.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python for loop continue and break
Python For Loop Continue And Break - Spark By {Examples}
April 17, 2024 - With the break statement, you will early exit from the loop and continue the execution of the first statement after the loop. # Exit the loop using break statement courses=["java","python","pandas","sparks"] for x in courses: print(x) if x == ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ break-continue-and-pass-in-python
Python - Loop Control Statements
January 9, 2025 - ... # Using For Loop for i in range(5): ... += 1 ... Python Continue statement is a loop control statement that forces to execute the next iteration of the loop while skipping the rest of the code inside the loop for the current iteration only, ...
๐ŸŒ
Real Python
realpython.com โ€บ python-while-loop
Python while Loops: Repeating Tasks Conditionally โ€“ Real Python
March 3, 2025 - Python provides two keywords that let you modify that behavior: break: Immediately terminates a loop. The program execution then proceeds with the first statement following the loop body. continue: Ends only the current iteration.
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Breaking/continuing out of multiple loops - #16 by PythonMillionaire - Ideas - Discussions on Python.org
August 23, 2022 - Remember that a good number of multi-break situations can be better handled by treating the entire loop as a search operation, refactoring it into a function, and having the multi-break become โ€œcool, we found the thing, return a resultโ€. So you ...
๐ŸŒ
H2K Infosys
h2kinfosys.com โ€บ blog โ€บ python break, continue and pass statements in loops
Python Break, Continue, and Pass Statements: The Ultimate Guide
January 5, 2026 - In Python, the Python Break statement applies only to the loop in which it is written that is, the innermost loop. Unlike languages such as Java or PHP, Python does not support labeled continue statements that allow you to skip directly to the ...
๐ŸŒ
Problem Solving with Python
problemsolvingwithpython.com โ€บ 09-Loops โ€บ 09.03-Break-and-Continue
Break and Continue - Problem Solving with Python
Remember the keyword break cause the program to exit a loop. continue is similar, but continue causes the program to stop the current iteration of the loop and start the next iteration at the top of the loop. A code section that uses continue in a for loop is below.
๐ŸŒ
ThePythonBook
pythoncompiler.io โ€บ home โ€บ thepythonbook โ€” python tutorials โ€บ python break, continue, pass: controlling loop flow
Python break, continue, pass: Controlling Loop Flow | ThePythonBook
October 30, 2025 - Python gives you three statements to control what happens inside a loop: break (stop the loop entirely), continue (skip to the next iteration), and pass (do nothing, just hold space).
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-break-out-of-multiple-loops-in-python
How to Break out of multiple loops in Python ? - GeeksforGeeks
February 24, 2023 - The if block must check the value of the flag variable and contain a break statement. If the flag variable is True, then the if block will be executed and will break out of the inner loop also.