Breaking out of nested loops

Peter Hansen peter at
Sat Jan 12 15:38:14 EST 2002

Dietmar Schwertberger wrote:
> IMHO "break n" (default n=1) wouldn't be ugly, at least less ugly than
> the workarounds...
> Example:
> for x in range(10):
>     for y in range(10):
>         for z in range(10):
>             if abc(x,y,z):
>                 break 3

But it's not maintainable.  Someone refactoring the code to insert another
loop might not notice or deal with the break properly.  At least a label
has the benefit of being an absolute target.

More information about the Python-list mailing list