How to exit two loops in python

  for row in b:
    for drug in drug_input:
      for brand in brand_names[drug]:

from the third loop how do i exit the current loop and go to the next value of for row in b: ?

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:

done = False
for x in xs:
    for y in ys:
        if bad:
            done = True
            break

    if done:
        break

This one will continue if no break was used. The else will be skipped over if there was a break, so it will see the next break. This approach has the benefit of not having to use a variable, but may be harder to read to some.

for x in xs:
    for y in ys:
        if bad:
            break
    else:
        continue

    break

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.

  • Split your method up into smaller, simpler methods.
  • Use a list comprehension and methods like all and any to avoid writing explicit loops.

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

for row in b:
   more_drugs = True
   for drug in drug_input:
      for brand in brand_names[drug]:
          if something:
              more_drugs = False
              break

      if not more_drugs:
          break

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

for row in b:
    for drug in drug_input:
        try:
            for brand in brand_names[drug]:
                if some_condition:
                    raise StopIteration
        except StopIteration:
            break

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]: //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]:

class LocalBreak[Exception]: pass

try:
   for i in ...:
       for h in ...:
           for j in ...:
              if should_break[j]:
                 raise LocalBreak[]
except LocalBreak:
   pass

answered Jul 29, 2010 at 0:21

Nathan ErnstNathan Ernst

4,43022 silver badges37 bronze badges

for a in aa:
     for b in bb:
         for c in cc:
            if c == a[b]:
                break
         else:
             continue
         break

Python supports for...else statements. else block is evaluated if the inner break is not fired.

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 return statement. That will force-exit out of any number of loops. For example:

def example[self, drug_input]:
    ok = False
    for x in drug_input["names"]:
        for y in range[drug_input["number_of_drugs"]]:
            for z in drug_input["list_of_drugs"]:
                # do stuff
                if ok:
                    return drug_costs

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:

inner_termination=False
for row in b:
    for drug in drug_input:
       for brand in brand_names[drug]:
           
           if break_condition:
               inner_termination=True
               break 
           
       if inner_termination:
           inner_termination=False
           break 

answered Jul 28, 2010 at 20:37

Caleb HattinghCaleb Hattingh

8,7462 gold badges29 silver badges43 bronze badges

for row in b:
    ok = True
    for drug in drug_input:
        if not ok:
            break;
        for brand in brand_names[drug]:
            if not ok:
                break
            if whatever:
                ok = False

answered Jul 28, 2010 at 20:39

Teodor PripoaeTeodor Pripoae

1,3581 gold badge11 silver badges26 bronze badges

try/except/raise, as suggested in @gddc's comment, is one possibility. Another one is to "wrap up" the two nested loops into one:

for row in b:
  for brand in [b for d in drug_input for b in brand_names[d]]:
    ...

now, a break from the for brand nested loop will go back to the for row outer loop level. Of course, this works only when the code that is here replaced by ... does not need to see the drug name bound to the drug currently being examined. Is that the case for you?

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.

Chủ Đề