breaking out of outer loops

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


noemailplease0001 at gmail.com 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.

Diez



More information about the Python-list mailing list