breaking out of outer loops

Diez B. Roggisch deets at
Tue Jan 29 23:45:07 CET 2008

noemailplease0001 at schrieb:
> Any elegant way of breaking out of the outer for loop than below, I
> seem to have come across something, but it escapes me
> for i in outerLoop:
>    for j in innerLoop:
>        if condition:
>           break
>    else:
>        continue
>     break

It's working because for-loops else statements are are executed only if 
the loop hasn't been terminated unexpectedly. Which is what happens 
here: if the inner loop is breaked, it's else is not executed. So the 
outer loop's break is called.


More information about the Python-list mailing list