๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ break-in-python-nested-for-loop-break-if-condition-met-example
Break in Python โ€“ Nested For Loop Break if Condition Met Example
May 17, 2022 - The example in this section will be similar to the last section's. We'll be using a while loop instead. ... The code above prints a range of numbers from 0 to 9. We're going to use the break to stop printing numbers when we get to 5. i = 1 while i < 10: print(i) if i == 5: break i += 1 ยท Just like we did in the last section, we created a new condition: if i == 5 and when this condition is met, the loop is terminated instead of printing all the way to 9.
Discussions

python - How to stop one or multiple for loop(s) - Stack Overflow
-1 How to skip a part of the code if a certain condition is met ยท -1 How can I stop the loop exatly when I get the result I want in Python? More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Stopping a function with a while loop when a certain condition is met - Stack Overflow
I basically want to know how I can stop the while-loop from adding to the list when it reaches a certain element of the list ("LA Dodgers Stadium") in this case. It should not add anything after it reaches that element of the list. I know I asked this question before but I did not recieve a proper answer. ... You might wish to alter you loop condition... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I end a For or While loop without using break?
A for loop ends when it exhausts its iterable, and a while loop terminates when its condition is no longer met. If you were going to write while True: if some_variable == "some value": break #do stuff then instead you could write: while some_variable != "some value": #do stuff More on reddit.com
๐ŸŒ r/learnpython
4
4
March 19, 2022
python - Stop for loop or while loop when condition met - Stack Overflow
I am newby in programming. The goal is to count numbers in list in order, but loop has to stop when condition is met or close to it, but must not exceed it. For example: list = [4,4,4,3,3], conditi... More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 26, 2021
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-use-break-continue-and-pass-statements-when-working-with-loops-in-python-3
How to Break Out of Multiple Loops in Python | DigitalOcean
August 7, 2025 - Wrapping your loop logic in a function and using return to exit is a Pythonic and efficient method to stop execution when a condition is met.
๐ŸŒ
Coursera
coursera.org โ€บ tutorials โ€บ python-break
How to Use Python Break | Coursera
In Python, break allows you to exit a loop when an external condition is met. Normal program execution resumes at the next statement. You can use a break statement with both for loops and while loops.
Top answer
1 of 7
129

Use break and continue to do this. Breaking nested loops can be done in Python using the following:

for a in range(...):
   for b in range(..):
      if some condition:
         # break the inner loop
         break
   else:
      # will be called if the previous loop did not end with a `break` 
      continue
   # but here we end up right after breaking the inner loop, so we can
   # simply break the outer loop as well
   break

Another way is to wrap everything in a function and use return to escape from the loop.

2 of 7
72

There are several ways to do it:

The simple Way: a sentinel variable

n = L[0][0]
m = len(A)
found = False
for i in range(m):
   if found:
      break
   for j in range(m):
     if L[i][j] != n: 
       found = True
       break

Pros: easy to understand Cons: additional conditional statement for every loop

The hacky Way: raising an exception

n = L[0][0]
m = len(A)

try:
  for x in range(3):
    for z in range(3):
     if L[i][j] != n: 
       raise StopIteration
except StopIteration:
   pass

Pros: very straightforward Cons: you use Exception outside of their semantic

The clean Way: make a function

def is_different_value(l, elem, size):
  for x in range(size):
    for z in range(size):
     if l[i][j] != elem: 
       return True
  return False
   
if is_different_value(L, L[0][0], len(A)):
  print "Doh"

pros: much cleaner and still efficient cons: yet feels like C

The pythonic way: use iteration as it should be

def is_different_value(iterable):
  first = iterable[0][0]
  for l in iterable:
    for elem in l:
       if elem != first: 
          return True
  return False
   
if is_different_value(L):
  print "Doh"

pros: still clean and efficient cons: you reinvdent the wheel

The guru way: use any():

def is_different_value(iterable):
  first = iterable[0][0]
  return any(cell != first for col in iterable for cell in col)

if is_different_value(L):
  print "Doh"

pros: you'll feel empowered with dark powers cons: people that will read you code may start to dislike you

๐ŸŒ
IONOS
ionos.com โ€บ digital guide โ€บ websites โ€บ web development โ€บ python break continue
How to use Python break and continue - IONOS
July 13, 2023 - A Python break which says that the loop should break when the number 5 is reached can then be inserted. 5 is within the specified range, meaning that the loop will terminate and the code will continue after that. It will look like this: for num in range(10): if num == 5: print("The termination condition is met") break print(f"The current number is {num}") print("Continuation after loop")
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-break-statement-tutorial
Python Break Statement โ€“ How to Break Out of a For Loop in Python
April 16, 2024 - Once again, "Jade" and "John" were printed because the loop stops when "John" is found. In this article, you learned to use the break statement in Python. You can use it to terminate the current loop when a condition is met.
Find elsewhere
๐ŸŒ
Real Python
realpython.com โ€บ python-while-loop
Python while Loops: Repeating Tasks Conditionally โ€“ Real Python
March 3, 2025 - The next script is almost identical to the one in the previous section except for the continue statement in place of break: ... This time, when number is 2, the continue statement terminates that iteration.
๐ŸŒ
Enki
enki.com โ€บ post โ€บ how-to-exit-a-loop-in-python
Enki | Blog - How to Exit a Loop in Python
Learn to manage loops effectively in Python using the break statement to exit for and while loops under specific conditions, optimizing code efficiency.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 69722136 โ€บ stop-for-loop-or-while-loop-when-condition-met
python - Stop for loop or while loop when condition met - Stack Overflow
October 26, 2021 - Use another variable name like l. Then, if you run the loop in a function, it is simpler to just return from inside the loop if the condition is met ... Save this answer. Show activity on this post.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python for loop continue and break
Python For Loop Continue And Break - Spark By {Examples}
April 17, 2024 - Note: continue statement doesnโ€™t exit the for/while loop. ... Sometimes you would like to exit from the python for/while loop when you meet certain conditions, using the break statement you can exit the loop when the condition meets.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ break-continue
Python break and continue (With Examples)
Try Programiz PRO! ... The break statement terminates the loop immediately when it's encountered. ... The above image shows the working of break statements in for and while loops. Note: The break statement is usually used inside decision-making statements such as if...else.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-break-statement
Python break statement - GeeksforGeeks
The break statement in Python is used to exit or "break" out of a loop (either for or while loop) prematurely, before the loop has iterated through all its items or reached its condition.
Published ย  3 weeks ago
๐ŸŒ
Replit
replit.com โ€บ home โ€บ discover โ€บ how to stop a loop in python
How to stop a loop in Python | Replit
February 13, 2026 - Once the condition is met, the flag is set to False. The loop then terminates gracefully after completing its current iteration. This approach offers a "soft" exit, allowing the rest of the code in the current iteration to run before stopping, ...
๐ŸŒ
LearnPython.com
learnpython.com โ€บ blog โ€บ end-loop-python
How to End Loops in Python | LearnPython.com
The break statement is the first of three loop control statements in Python. It is used in conjunction with conditional statements (if-elif-else) to terminate the loop early if some condition is met.