Featurecore
Exiting nested loops
Also called break out of nested loops, labeled break in Python, break two loops, break outer loop, PEP 3136.
break ends only the innermost loop, and Python has no labeled break. To leave several loops, return from a function, use itertools.product to make one loop, or use the else clause of the inner loop.
- Language
- Python
Synopsis
def find(grid, target):
for r, row in enumerate(grid):
for c, value in enumerate(row):
if value == target:
return r, c
return None
grid = [[1, 2, 3], [4, 5, 6]]
print(find(grid, 5), find(grid, 9))
(1, 1) None
return leaves both loops at once. This is the most direct way to stop a nested search.
Semantics
r1. break "terminates the nearest enclosing loop".1 PEP 3136 says: "In Python currently, break and continue can apply only to the innermost enclosing loop."2 Example: inner-only.
r2. PEP 3136 proposed labels for break and continue. Its notice says: "This PEP is rejected."2
r3. A return statement in a function ends all the loops of the function. Example: function-return.
r4. itertools.product is "Roughly equivalent to nested for-loops in a generator expression. For example, product(A, B) returns the same as ((x,y) for x in A for y in B)."3 So two nested loops become one loop, and one break ends it. Example: product.
r5. "In either kind of loop, the else clause is not executed if the loop was terminated by a break."4 An inner loop with else: continue followed by break passes a break out to the outer loop. Example: else-continue.
Examples
from itertools import product
for a, b in product(range(1, 10), repeat=2):
if a * b == 12 and a < b:
print(a, b)
break
2 6
grid = [[1, 2], [3, 4], [5, 6]]
for row in grid:
for value in row:
if value == 4:
print("found", value)
break
else:
continue
break
print("row", row)
found 4
row [3, 4]
When the inner loop ends with no break, continue goes to the next row. When the inner loop breaks, Python skips the else clause and runs the second break, which ends the outer loop (rule r5).
found = None
for row in [[1, 2], [3, 4]]:
for value in row:
if value > 2:
found = value
break
if found is not None:
break
print(found)
3
A flag variable works too, but it needs a second test in the outer loop.
Pitfalls
A single break does not end the outer loop:
This code runs but gives a wrong answer on purpose.
for row in [[1, 2], [3, 4]]:
for value in row:
if value == 1:
break
print(value)
print("next row")
next row
3
4
next row
The break ends only the inner loop for the first row. The outer loop goes on with the second row (rule r1).
Comparison
Go has labels. A labeled statement "may be the target of a goto, break or continue statement."5 For break, "If there is a label, it must be that of an enclosing" for, switch or select statement, "and that is the one whose execution terminates."5 So break OuterLoop in Go ends the outer loop directly. Python has no labels (rule r2), so a Python program uses one of the techniques of this page.
References
- The Python Language Reference, Python 3.15, 7.9. The break statement
- PEP 3136 - Labeled break and continue, Status; Rejection Notice; Introduction
- itertools - Functions creating iterators for efficient looping, Python 3.15 documentation, itertools.product()
- The Python Tutorial, 4. More Control Flow Tools, 4.5. else Clauses on Loops
- The Go Programming Language Specification, Break statements; Labeled statements
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag B0J8TC Edit this page History Report an error Markdown