Logo

Programming-Idioms

History of Idiom 43 > diff from v13 to v14

Edit summary for version 14 by :

Version 13

2015-08-21, 16:52:29

Version 14

2015-08-22, 10:33:44

Idiom #43 Break outer loop

Look for a negative value v in 2D integer matrix m. Print it and stop searching.

Idiom #43 Break outer loop

Look for a negative value v in 2D integer matrix m. Print it and stop searching.

Code
class BreakOuterLoop (Exception): pass

try:
    position = None
    for row in m:
        for column in m[row]:
            if m[row][column] == v:
                position = (row, column)
                raise BreakOuterLoop
except BreakOuterLoop:
    pass
Comments bubble
This is ugly because the pythonic way to solve this problem would be to refactor so that one doesn't have to break out of multiple loops. See PEP3136
Origin
http://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops-in-python