Programming-Idioms

History of Idiom 43 > diff from v38 to v39

Edit summary for version 39 :
New Python implementation by user [Rochelimit]

Version 38

2018-12-17, 19:45:20

Version 39

2019-09-26, 14:22:03

Idiom #43 Break outer loop

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

Illustration

Idiom #43 Break outer loop

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

Illustration
Code
def loop_breaking(m, v): 
     position = None 
     for row in range(len(m)): 
         for column in range(len(m[row])): 
             if m[row][column] == v: 
                 position = (row, column) 
                 return position
     return position

print(loop_breaking(([1,2,3],[4,5,6],[7,8,9]), 6))
Comments bubble
Rather than set break flags, it is better to refactor into a function, then use return to break from all nested loops.