break out of nested loop python code example
Example 1: how to exit a double loop in python
for word1 in buf1:
find = False
for word2 in buf2:
...
if res == res1:
print "BINGO " + word1 + ":" + word2
find = True
break # <-- break here too
if find:
break
Example 2: how to break out of nested loops python
x_loop_must_break = False
for x in [1, 2, 3]:
print(f"x is {x}")
for y in [1, 2, 3]:
print(f"y is {y}")
if y == 2:
x_loop_must_break = True
break
if x_loop_must_break: break