- Break from the inner loop (if there's nothing else after it)
- Put the outer loop's body in a function and return from the function
- Raise an exception and catch it at the outer level
- Set a flag, break from the inner loop and test it at an outer level.
- Refactor the code so you no longer have to do this.
I would go with 5 every time.
Answer from Duncan on Stack Overflow- Break from the inner loop (if there's nothing else after it)
- Put the outer loop's body in a function and return from the function
- Raise an exception and catch it at the outer level
- Set a flag, break from the inner loop and test it at an outer level.
- Refactor the code so you no longer have to do this.
I would go with 5 every time.
Here's a bunch of hacky ways to do it:
Create a local function
for a in b: def doWork(): for c in d: for e in f: if somecondition: return # <continue the for a in b loop?> doWork()A better option would be to move doWork somewhere else and pass its state as arguments.
Use an exception
class StopLookingForThings(Exception): pass for a in b: try: for c in d: for e in f: if somecondition: raise StopLookingForThings() except StopLookingForThings: pass
Python: Continuing to next iteration in outer loop - Stack Overflow
Using Continue Statement in nested for loop in Python - Stack Overflow
python - How to continue a loop while being in a nested loop? - Stack Overflow
Nested for loops - how to leave inner loop, to go to outer loop's next iteration, but also continue inner loop's iteration?
Videos
Hi! I don't have much experience (around 3 years of not full-time programming) and was wondering what do you think about the use of 'continue' statement in for loops
In the project I'm working on there is a need to make a lot of validations.What usually happens we program using a lot of 'if' and 'elif' and 'else'So the indentations make the code wider than a Dubai highway...
What I thought about is calling methods for error controlling (logs the issue and returns False)and using continue for the next item in the loop
What do you think?
(Some colleagues tried to create different Classes for each of the validation but IMO it gets the code too twisted and practically impossible to debug - may work in a bigger project)
Copyfor 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).
Copyfor 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
Copydef 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?)
Copyfor 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.
Copyclass 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.