Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
breaking outof nexted loops
#1
one of the many coding difficulties i encounter in most languages is breaking out of 2 or more nested loops. in C i did have a goto but that was considered bad code. i do recall there was a language that allowed break to have a number (always must be a hard coded literal constant) to specify the number of loop levels to break out of. the usual way is to break out of each level one at a time based on a specific variable or specific value(s). constrained types like bytearray can sometimes force an extra variable.

what coding techniques to do things like this have you come up with?
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
Can you refactor your code to use single loop to iterate over itertools.product instead of having nested loops?
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
#3
Suggested practice is the use of 'return': Guido rejecting PEP 3136 -- Labeled break and continue.

Of course one can break out of nested loop but this is not something which is easy to understand - using for-loop with else and then break:

for loop:                 # outer loop
    # do something
    for loop:             # inner loop
        # do something
        # outcome: break or no-break
    else:                 # no-break
        continue          # go back to outer loop
    break                 # break occured in inner loop, break outer loop as well
 
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Skaperen Wrote:what coding techniques to do things like this have you come up with?
A simple way to exit a deeply nested loop in python is to raise an exception. In fact I once wrote a thread in this forum with a 'context' specially designed to exit deeply nested loops, even across functions boundaries. It remains a case study, because I don't use it in practice.
Reply
#5
i could use return if the context change could be dealt with. i do like the idea of exceptions but i think refactoring to use itertools.product would be best for cases where that can be done.
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
  ChromeDriver breaking Python script genericusername12414 1 218 Mar-14-2024, 09:39 AM
Last Post: snippsat
  Openpyxl module breaking my code EnderSM 5 986 May-26-2023, 07:26 PM
Last Post: snippsat
  breaking out of nested loops Skaperen 3 1,175 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  How to use += after breaking for loop? Frankduc 2 1,448 Dec-31-2021, 01:23 PM
Last Post: Frankduc
  How to fix while loop breaking off raphy11574 3 2,127 Oct-12-2021, 12:56 PM
Last Post: deanhystad
  breaking a large program into functions, not acting as expected Zane217 9 2,932 Sep-18-2021, 12:37 AM
Last Post: Zane217
  Need to run 100+ Chrome’s with Selenium but can’t get past ~15 without breaking imillz 0 1,322 Sep-04-2021, 04:51 PM
Last Post: imillz
Exclamation Help in breaking bytes into bits through masking and shifting kamui123 9 4,440 Jan-11-2021, 07:42 AM
Last Post: kamui123
  Breaking While Loop JustWonderful 4 3,013 Oct-28-2019, 01:12 AM
Last Post: JustWonderful
  (Python help) Change in logic not breaking 'while' loop? btcg2807 1 1,863 Sep-18-2019, 09:43 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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