How to exit two loops in python
from the third loop how do i exit the current loop and go to the next value of Show asked Jul 28, 2010 at 20:33
Alex GordonAlex Gordon 55k281 gold badges656 silver badges1037 bronze badges 8 This one uses a boolean to see if you are done yet:
This one will
answered Jul 28, 2010 at 20:39
Donald MinerDonald Miner 37.7k8 gold badges90 silver badges117 bronze badges 4 If you have three levels of looping in one method then you probably need to rethink your design.
Doing either of these should mean that you no longer have this issue. answered Jul 28, 2010 at 20:38
Mark ByersMark Byers 782k184 gold badges1552 silver badges1440 bronze badges 6
Python doesn't have a control structure for breaking from two loops at once, so you need to do something manual like this. answered Jul 28, 2010 at 20:37
Ned BatchelderNed Batchelder 351k71 gold badges552 silver badges650 bronze badges 1 Exception handling beats setting variables all over the place IMO
answered Jul 28, 2010 at 21:00
6
I would consider putting the two inner loops in function and using return from there. Probably rethinking what you are doing and how gives the better alternative to that though. Could you give your current pseudo code, input and output, so we could try to remove the need for the break in first place? We need to see where the loop variables are used or better still, what is the goal of the processing. answered Jul 28, 2010 at 20:52
Latest PEP I see requesting this feature is 3136 (and was rejected): http://mail.python.org/pipermail/python-3000/2007-July/008663.html Closest & cleanest thing I could see to what you want to do would be do the following (and since even type names are scoped, you could declare LocalBreak within the function its needed):
answered Jul 29, 2010 at 0:21
Nathan ErnstNathan Ernst 4,43022 silver badges37 bronze badges
Python supports answered Sep 11, 2012 at 14:54
1 If you have too many embedded loops, it might be time for a refactor. In this case, I believe the best refactor is to move your loops into a function and use a
Now, perhaps reformulating your logic like this is tricky, but I bet the refactoring will help in other ways. answered Feb 28, 2013 at 6:48
john_sciencejohn_science 6,1576 gold badges42 silver badges58 bronze badges Untested:
answered Jul 28, 2010 at 20:37
Caleb HattinghCaleb Hattingh 8,7462 gold badges29 silver badges43 bronze badges
answered Jul 28, 2010 at 20:39
Teodor PripoaeTeodor Pripoae 1,3581 gold badge11 silver badges26 bronze badges
now, a answered Jul 28, 2010 at 20:40
Alex MartelliAlex Martelli 825k163 gold badges1203 silver badges1380 bronze badges 1 How do you exit multiple loops in Python?Method 1: Using the return statement
Define a function and place the loops within that function. Using a return statement can directly end the function, thus breaking out of all the loops.
How do you exit multiple loops?The best options are: Set a flag which is checked by the outer loop, or set the outer loops condition. Put the loop in a function and use return to break out of all the loops at once. Reformulate your logic.
How do you break out of a loop in Python?Python provides two keywords that terminate a loop iteration prematurely: The Python break statement immediately terminates a loop entirely. Program execution proceeds to the first statement following the loop body. The Python continue statement immediately terminates the current loop iteration.
|