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.

Answer from Alexander Gessler on Stack Overflow
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-break-continue-and-pass-statements-when-working-with-loops-in-python-3
How To Use break, continue, and pass Statements in Python | DigitalOcean
April 24, 2026 - Learn how to break out of nested loops in Python, exit specific loop levels using flags, functions, exceptions, and best practices for clean loop control
Discussions

python - Loop stops after first iteration - Stack Overflow
I need to check if every string in a list is in titlecase. If yes return True - if not return False. I have written the following: word_list=["ABC", "abc", "Abc"] def More on stackoverflow.com
🌐 stackoverflow.com
How can we exit a loop?
Question In the context of this code challenge, how can we exit a loop? Answer In Python, the main way to exit a loop is using the break statement. When the break statement runs in a loop, it will terminate that loop. However, one thing to keep in mind is that break statements will only terminate ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
10
November 3, 2018
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
For Loop for n iterations-Python - Stack Overflow
I have a simple for loop in a Python script: for filename in filenames: outline = getinfo(filename) outfile.write(outline) This for loop is part of a larger script that extracts data from ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 - Above is a Python for loop that iterates over a list of names and prints all the names. In situations where we want to stop the iteration before getting to the last item or before a given condition is met, we can use the break statement.
🌐
Finxter
blog.finxter.com › home › learn python blog › how to stop a for loop in python
How to Stop a For Loop in Python - Be on the Right Side of Change
September 28, 2022 - The most natural way to end a Python for loop is to deplete the iterator defined in the loop expression for <var> in <iterator>. If the iterator’s next() method doesn’t return a value anymore, the program proceeds with the next statement ...
🌐
LearnPython.com
learnpython.com › blog › end-loop-python
How to End Loops in Python | LearnPython.com
December 16, 2021 - In the example above, we progress through the sequence starting at -2, taking steps of 3, and then we stop at 10. This is before the defined stop value of 11, but an additional step of 3 takes us beyond the stop value. This is the most obvious way to end a loop in Python – after a pre-defined number of iterations.
🌐
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.
Find elsewhere
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › python faq
How can we exit a loop? - Python FAQ - Codecademy Forums
November 3, 2018 - Question In the context of this code challenge, how can we exit a loop? Answer In Python, the main way to exit a loop is using the break statement. When the break statement runs in a loop, it will terminate that loop.
🌐
Real Python
realpython.com › python-break
How to Exit Loops Early With the Python break Keyword – Real Python
November 1, 2025 - A break in Python is a keyword that lets you exit a loop immediately, stopping further iterations. Using break outside of loops doesn’t make sense because it’s specifically designed to exit loops early.
🌐
Stack Overflow
stackoverflow.com › questions › 68801812 › how-can-i-amend-my-for-loop-so-that-it-does-not-stop-after-just-one-iteration
python - How can I amend my for-loop so that it does not stop after just one iteration? - Stack Overflow
August 16, 2021 - Add from time import sleep at the beginning of your code. Then in the inner for loop, after you make a request add sleep(1). This will stop the program for 1 second.
🌐
BlueVPS
bluevps.com › blog › how to end a loop in python? (python exit for loop)
How to End a Loop in Python?
September 9, 2025 - The break statement allows you to stop a loop immediately when a specific condition is met. Once triggered, the loop ends and the program continues with the next section of code. The continue statement, on the other hand, doesn’t stop the ...
🌐
CodeRivers
coderivers.org › blog › stop-a-loop-python
Stopping Loops in Python: A Comprehensive Guide - CodeRivers
February 22, 2026 - You can also change the termination condition of a loop. Consider the following example: terminate = False numbers = [1, 2, 3, 4, 5] for num in numbers: if num == 3: terminate = True if not terminate: print(num) In this code, when num is 3, we set terminate to True. Then, in the next iteration, the if not terminate condition fails, and the loop effectively stops printing numbers.
🌐
GeeksforGeeks
geeksforgeeks.org › python › skip-iterations-in-for-loop-python
How to skip Iterations in For Loop - Python - GeeksforGeeks
July 23, 2025 - In a for loop, you can use continue to skip a specific iteration when a condition is true. When Python sees continue, it skips the rest of the current iteration and moves to the next iteration.
🌐
freeCodeCamp
freecodecamp.org › news › python-break-and-python-continue-how-to-skip-to-the-next-function
Python Break and Python Continue – How to Skip to the Next Function
March 14, 2022 - We could add a condition inside our while loop that says if num is 9, then break out of the loop. num = 5 while num < 20: print('Current number :', num) num = num + 1 if num == 9: break ... You can use the continue statement if you need to skip ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-break-statement
Python Break Statement - GeeksforGeeks
The break statement can be used within a for loop to exit the loop before it has iterated over all items, based on a specified condition. ... A while loop in Python repeatedly executes a block of code as long as a specified condition is True.
Published: June 5, 2026
🌐
Coursera
coursera.org › tutorials › how to use python break
How to Use Python Break | Coursera
February 24, 2023 - Break is a loop control statement along with continue and pass. You can use break to exit for loops and while loops. Break only exits the innermost loop in a nested loop. You can’t use break to exit an if statement unless the if statement ...
🌐
Stack Overflow
stackoverflow.com › questions › 51655172 › why-does-my-loop-stop-after-one-iteration
python - Why does my loop stop after one iteration? - Stack Overflow
Later edit: I have a folder "x" with 2 files "f1" and "f2"; "f1" contains the numbers 1,2,3 one per line while "f2" contains the numbers 4,5,6 · >>> print open_data(".\\x") # gives (2, ['1\n', '2\n', '3\n', '4\n', '5\n', '6\n']) using the generator, you won't have the list of all the lines, but an "iterable", you could call it "lazy-reader", in order to use it you have to iterate over it · >>> for line in get_lines(".\\x"): ...