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.
My code examples are always for Python >=3.6.0
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
  How to break out of nested loops pace 11 1,311 Mar-03-2021, 06:25 PM
Last Post: pace
  Nested for Loops sammay 1 455 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 975 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Python beginner - nested while loops mikebarden 1 579 Jun-01-2020, 01:04 PM
Last Post: DPaul
  alternative to nested loops for large data set JonnyEnglish 2 862 Feb-19-2020, 11:26 PM
Last Post: JonnyEnglish
  Nested for loops d79danny 8 1,614 Oct-05-2019, 02:12 AM
Last Post: newbieAuggie2019
  Printing out a triangle using nested for loops MrGoat 12 2,906 Jan-16-2019, 07:21 PM
Last Post: ichabod801
  Looping and nested loops albry 5 1,959 Dec-20-2018, 12:10 PM
Last Post: buran
  nested for loops to recursion ashkea26 1 1,463 Nov-02-2018, 09:53 AM
Last Post: Larz60+
  nested for loops glob devenuro 3 2,632 Sep-20-2018, 09:54 PM
Last Post: ODIS

Forum Jump:

User Panel Messages

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