0

Possible Duplicate:
Python: Continuing to next iteration in outer loop

Maybe the title is kind of confusing but you will understand what i mean in the code:

for item in items: #i want to skip one loop of this bucle
    for i in item: #loop nº2
        if i==5:
            continue #but this only skip a loop in nº2, there's no propagation

How can i get this to work? Thanks in advance.

0

1 Answer 1

4

Set a flag, break out of the inner loop, check the flag in the main loop, and continue as appropriate.

  for i = 1 to N do
     flag = false
     for j = 1 to M do
        ...
        if condition then
           flag = true
           break
        ...
     if flag then continue
     ...
Sign up to request clarification or add additional context in comments.

8 Comments

Yep, that should work, but i thought that maybe there was another more-pythonic way...
In general, I'd recommend you not worry about what is "Pythonic".
@Thomas: Quite the contrary, I'd highly recommend learning idioms and applying them whenever sensible. (Of course there are also claims of this or that being "pythonic" that are at least fishy.)
The issue that "break" can only exit the innermost while/for loop comes up periodically. From what I've gathered, all the solutions proposed to date have been somewhat complex, requiring core modifications to the grammar, which the Python dev team has deemed "not worth it" given the comparative rarity of the use-case. So Thomas's solution is in fact the commonly accepted way to go, as aesthetically unpleasing as it is :)
What you can do in that case is break out the parsing of the table cell (which I gather involves some kind of loop) into its own function, which returns the flag (or raises an exception).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.