Python Forum
best way out of nested loops?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
best way out of nested loops?
#1
the best and most Pythonic way (in your opinion) to get all the way out of a few nested loops is?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Break statement I guess? Or if you're looking for something more drastic, sys.exit?
Reply
#3
maybe look for way to refactor code without nested loops (can you use itertools.product and break?)?
maybe separate loops in function and return?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(May-27-2020, 07:21 AM)Knight18 Wrote: Break statement I guess?
just break will break out of the one specific loop, but the question is about nested loops and break out all the way
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
My kludge would be to set a flag in that innermost loop, and then test for the flag when coming out (and break again). This works:
for loop1 in range(10):
    for loop2 in range(10):
        for loop3 in range(10):
            for loop4 in range(10):
                x = input('break')
                flag = True
                break
            if flag:
                break
            #do other stuff
        if flag:
            break
        #do other stuff
    if flag:
        break
    #do more other stuff
Reply
#6
@jefsummers your solution is a candidate for refactoring.

def loop():
    for i in range(10):
        for j in range(10):
            if i*j > 10:
                return i, j
Just put everything in a function, return if the condition is fulfilled.
If tasks afterwards are necessary, you could work with the return value of the loop function.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
I would try to flatten the logic if I could. Failing that I would try to "return" out. Failing that I would raise an exception.
Reply
#8
i like the function idea. but that has the context problem where you might need to pass lots of data. that is where refactoring is needed. i think raising an exception is a kludge. exceptions have a purpose and this is not it. still, it can be a quick way to get at this kind of issue when the cost (such as time) of refactoring is too high.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reduce nested for-loops Phaze90 11 1,757 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,638 Aug-19-2022, 11:07 AM
Last Post: dm222
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,531 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  breaking out of nested loops Skaperen 3 1,174 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  Break out of nested loops muzikman 11 3,239 Sep-18-2021, 12:59 PM
Last Post: muzikman
  How to break out of nested loops pace 11 5,262 Mar-03-2021, 06:25 PM
Last Post: pace
  Nested for Loops sammay 1 7,277 Jan-09-2021, 06:48 PM
Last Post: deanhystad
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,330 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Python beginner - nested while loops mikebarden 1 1,838 Jun-01-2020, 01:04 PM
Last Post: DPaul
  alternative to nested loops for large data set JonnyEnglish 2 2,522 Feb-19-2020, 11:26 PM
Last Post: JonnyEnglish

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020