To break out of nested loops in Python, wrap the loops in a function and use return, or raise a private exception, or use a flag variable, or put an else clause on the inner loop. A plain break only leaves the innermost loop, so Python has no built-in "break 2" statement. The cleanest option in most programs is return from a small helper function.
This guide shows each technique with a complete example for Python 3.13 and 3.14, explains when each one fits, and lists the pitfalls that make nested-loop exits hard to read.
A break statement ends the loop that directly contains it. When that loop sits inside another loop, the outer loop keeps running with its next iteration.
The inner loop stops at col == 1 every time, but the outer loop still visits all three rows. Python deliberately has no labelled break (as Java has) or goto, so you restructure the code instead.
If the loops search for something, move them into a function and return the moment you find it. return leaves every loop at once and also hands back the result.
This is usually the best answer: no extra state, no flags, and the function name documents the intent. If the loops are currently inline in a long function, extracting them is a small refactor that tends to improve the code anyway.
A boolean flag works when you cannot extract a function. Set it before break in the inner loop, then check it in the outer loop.
The flag costs one extra variable and one extra if per outer loop. With three or more levels, you need a check after every level, and the code gets noisy fast. Use flags for two levels at most.
A for loop can have an else block that runs only when the loop finishes without hitting break. Combined with continue, it lets the outer loop react to an inner break without a flag.
This is compact and correct, but many readers find it cryptic. Add a comment if you use it, and prefer a function when the logic is more than a few lines.
You can raise a custom exception inside the innermost loop and catch it outside all the loops.
Exceptions are fast enough for this use, but they are meant for exceptional control flow. Use this only when the loops are deep (three or more levels) and a function is not practical. Always define your own exception class so you do not accidentally catch an unrelated error.
Sometimes the nesting itself is the problem. If you only need a single break condition over all combinations, iterate over a single flat iterator and one break is enough.
itertools.product yields the same pairs in the same order as the nested loops, so one break ends the whole search. The same idea works with a generator expression and next():
This only works when the inner loop's range does not depend on side effects of the outer loop body. If there is real work between the loops, use one of the earlier methods.
| Method | Best for | Drawback |
|---|---|---|
return from a function | Searches, most real code | Needs a function boundary |
| Flag variable | Two levels, inline code | Extra state, repeated checks |
for-else with continue | Short two-level loops | Hard to read |
| Custom exception | Three or more levels | Control flow by exception |
itertools.product or next() | Pure searches over combinations | No code between levels |
No. Python has neither, and the language has no plans to add them. Use a function with return, or one of the other patterns above.
Yes. continue skips to the next iteration of the innermost loop only. To skip to the next outer iteration, break the inner loop (and use for-else or a flag if you need to know about it).
Raising and catching one exception costs microseconds, which is irrelevant unless it happens millions of times. The real cost is readability, so keep it for deep nesting.
Yes. Every technique here works for any mix of for and while loops. A while loop also supports an else clause.
break leaves only the innermost loop. The most readable way to leave all loops is to wrap them in a function and return. Use a flag for simple two-level cases, for-else with continue when you want to avoid extra state, a custom exception for deep nesting, and itertools.product when the nesting is just a way of enumerating combinations.