Breaking out of nested loops

Ulf Magnusson ulf.magnusson at
Sat Jan 12 09:55:24 EST 2002

"Tal Linzen" <pachy at> wrote in message
news:mailman.1010834178.14290.python-list at
> Good day.
> One feature I miss in python is the ability to break out of nested loops.
> I've searched the newsgroup archives, and found a few threads discussing
> problem, with no apparent conclusion. The two solutions I've seen --
> an exception, or putting the loop inside a function and using the return
> statement -- are unintuitive (meaning, I didn't come up with them myself
> :-) ).
> Know if anyone has drafted a PEP for a syntactic solution for this
> Tal

I guess you mean something like this
for i in ...:
    for j in ....:
         if some_condition:
code continues here...

There are not supposed to be such a magical break, to my knowledge no
other language support this either. It kind of breaks the good behaviour you
expect from a imparative programming language.

The best solution is to set a flag inside the inner for loop and check for
the first thing in the outer for loop. Exceptions should only be used for
real exceptions when something is wrong, not to change the flow of the

But this is of course my humble opinion

Do I make any sence?

/U. Magnusson

More information about the Python-list mailing list