Using itertools.count:

import itertools
for i in itertools.count(start=1):
    if there_is_a_reason_to_break(i):
        break

In Python 2, range() and xrange() were limited to sys.maxsize. In Python 3 range() can go much higher, though not to infinity:

import sys
for i in range(sys.maxsize**10):  # you could go even higher if you really want
    if there_is_a_reason_to_break(i):
        break

So it's probably best to use count().

Answer from John La Rooy on Stack Overflow
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
"for i in range()" to do an infinite loop with a counter - Ideas - Discussions on Python.org
August 10, 2022 - Hi, Usually in Python we can avoid the i = 0 โ€ฆ i += 1 paradigm that we use in other languages when we need to count things, thanks to enumerate(...), for i in range(100), etc. Along the years I have nearly always found a more โ€œpythonicโ€ replacement for code containing i = 0 โ€ฆ i += 1. There is an exception with this code: an infinite loop with a counter: i = 0 while True: ... if breaking_condition: break i += 1 Proposal: could we accept that range() without any parameter ...
Discussions

Infinite loops using 'for' in Python - Stack Overflow
This loop will indeed run infinitely. ... This will however easily run out of memory. Furthermore some collections (not lists) will raise an exception for good reasons when altering a collection when iterating over it. 2018-01-11T12:07:45.473Z+00:00 ... Sure. It's not meant for actual "use", just for creating a counter-example which behaves as OP expected. 2018-01-11T12:08:35.727Z+00:00 ... Because a range is either a list (Python2... More on stackoverflow.com
๐ŸŒ stackoverflow.com
January 11, 2018
[Thought experiment] Achieving one-line infinite loops in Python
One-liner infinite print loop, that doesn't use while, append, etc.: for i in __import__('itertools').count(): print(i) More on reddit.com
๐ŸŒ r/learnpython
15
5
September 5, 2018
Allow `range(start, None, step)` for an endless range - Ideas - Discussions on Python.org
Currently, to iterate over finite arithmetic sequences of integers, range is used, as in: for i in range(10): print(i) For an infinite arithmetic sequence, there are a few approaches. One can replace the โ€˜10โ€™ with a long sequence of nines, write a generator function or just switch to a ... More on discuss.python.org
๐ŸŒ discuss.python.org
0
January 15, 2021
How do I make an infinite for loop in Python (without using a while loop)? - Stack Overflow
Is there way to write an infinite for loop in Python? for t in range(0,10): if(t == 9): t= 0 # will this set t to 0 and launch infinite loop? No! print(t) Generally speaking, is there way to More on stackoverflow.com
๐ŸŒ stackoverflow.com
April 29, 2016
๐ŸŒ
The Python Coding Stack
thepythoncodingstack.com โ€บ p โ€บ infinite-for-loop-infinite-iterator-python
To Infinity and Beyond โ€ข The Infinite `for` Loop
March 7, 2024 - If you need to refresh your memory about what iterators are and the difference between iterators and iterablesโ€”they're not the sameโ€”you can read A One-Way Stream of Data โ€ข Iterators in Python. You can import itertools and update all_positions to assign the infinite cycle iterator to it instead of a finite list: ... Run this code and you'll get an infinite animation. The for loop iterates through all_positions, but all_positions is an infinite iterator.
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ while-loops-infinite-range-function-python-baha-yaf9f
while loops, infinite loops, for loops, and the range function - Python
October 28, 2024 - This is why range(5) results in five iterations through the clause, with i being set to 0, then 1, then 2, then 3, and then 4. The variable i will go up to, but will not include, the integer passed to range(). Two ways to execute a block of code a certain number of times ... We can use a while loop to do the same thing as a for loop. Letโ€™s rewrite fivetimes.py to use a while loop equivalent of a for loop. ... Running this code gives the same output as the for loop example 1. ... We want to add up all the numbers from 0 to 100. We can write a Python program with a for loop to do this calculation for us.
๐ŸŒ
Learning About Electronics
learningaboutelectronics.com โ€บ Articles โ€บ How-to-create-an-infinite-for-loop-in-Python.php
How to Create an Infinite For Loop in Python
Using the the range() function in Python, we can set up a range that goes from one value to a certain value, such as 1 to 3, and then have this repeat infinitely using the cycle() function from the itertool module. This way, we can create an infinite loop between a certain range in Python.
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ Can-the-for-loop-be-used-to-create-an-infinite-loop-in-Python
Can the for loop be used to create an infinite loop in Python? - Quora
Answer (1 of 6): At a first glance, you can easily loop infinitely using a for-loop in Python. Just keep adding to the iterable that feeds your loop. For example: [code]iterable = ['the', 'end', 'is', 'never'] for word in iterable: print(word) iterable.append(word) [/code]However, this ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ [thought experiment] achieving one-line infinite loops in python
r/learnpython on Reddit: [Thought experiment] Achieving one-line infinite loops in Python
September 5, 2018 -

DISCLAIMER: This post is mainly just curious thoughts, it has nothing to do with real-life application or good practice. So please don't actually use any examples provided.

Python is (or at least was) rather famous for its possibilities for one-liners (programs occupying only a single line of code) some time ago. A lot of things can be achieved like this, but among the most puzzling things must be infinite loops; they aren't exactly easy to implement with the tools we have available.

An infinite loop usually requires the use of a while-loop, because for-loops have a beginning and an end. Using a while-loop in one-liners is problematic, though, because you may only use it once, on the top level. This is due to how Python restricts block structures to either be separated by whitespace (and proper indentation), or to only have a single depth level following it. In other words,

while True: print("This works!")

is valid Python, but

while True: if 1 == 1: print("But this doesn't...)

is not.

We do have another "kind" of loop, though; list comprehensions. They are unique in that they may be nested as we see fit, all while using only a single line.

[["Order pizza." for _ in range(6)] for _ in range(42)]

But this doesn't give us an infinite loop; even if we simply input a ridiculously large number to range, it's still technically finite no matter what kind of hardware we're using. Thus, a different approach is required. I mentioned how infinite loops usually require the use of while-loops in Python. We can, however, utilise a certain property of Python to create an infinite loop with for-loops.

nums = [1, 2, 3, 4]
for num in nums:
    print(num)

Okay, that prints out four numbers. Not exactly infinite. But if we tweak our approach a little...

nums = [1]
for num in nums:
    print(num)
    nums.append(num + 1)

We actually get... as many numbers as the computer's memory allows. With this, we can essentially get something like this to work:

nums=[1];[(print(num) and nums.append(num+1)) for num in nums]

(Disclaimer; I never tested if that actually runs.)

It's not a pure one-liner, because it still technically requires two lines (fused together with a semicolon), but it's a proof-of-concept. I initially tried to make it work without having to define a variable, but failed to find a way.

I hope this was mildly interesting, I don't usually write stuff like this. Just found it curious myself, so why not share the thought? Maybe someone can even improve on this.

๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ looping-technique
Python Looping Techniques
We can create an infinite loop using while statement. If the condition of while loop is always True, we get an infinite loop. # An example of infinite loop # press Ctrl + c to exit from the loop while True: num = int(input("Enter an integer: ")) print("The double of",num,"is",2 * num)
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Allow `range(start, None, step)` for an endless range - Ideas - Discussions on Python.org
January 15, 2021 - Currently, to iterate over finite arithmetic sequences of integers, range is used, as in: for i in range(10): print(i) For an infinite arithmetic sequence, there are a few approaches. One can replace the โ€˜10โ€™ with a long sequence of nines, write a generator function or just switch to a while loop, but the current โ€˜bestโ€™ way is using itertools: import itertools for i in itertools.count(): print(i) There are a few downsides to this.
๐ŸŒ
Quora
quora.com โ€บ In-Python-how-do-I-set-the-range-of-a-for-loop-to-repeat-until-a-break-is-reached
In Python, how do I set the range of a for loop to repeat until a break is reached? - Quora
As in othersโ€™ answers, it is not possible to set a range object to have an infinite loop. I am wondering what could be a use-case for such a request of yours. However, you could set a sufficiently large number in the range of 10**300000 or so ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-loop-through-a-range
Python - Loop Through a Range - GeeksforGeeks
July 23, 2025 - In this article, we will explore the different ways to loop through a range in Python, demonstrating how we customize start, end, and step values, as well as alternative methods for more advanced use cases like looping through floating-point ranges or infinite sequences.
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
"for i in range()" to do an infinite loop with a counter - #13 by ncoghlan - Ideas - Discussions on Python.org
August 17, 2022 - Hi, Usually in Python we can avoid the i = 0 โ€ฆ i += 1 paradigm that we use in other languages when we need to count things, thanks to enumerate(...), for i in range(100), etc. Along the years I have nearly always foundโ€ฆ
๐ŸŒ
Vaia
vaia.com โ€บ python infinite loop
Python Infinite Loop: Definition & Examples | Vaia
Encountering an infinite loop can be confusing without understanding how they manifest. Here are several Python infinite loop examples: Iterating Over a Mutable Collection: If you modify a list while iterating over it, this might result in endless iteration. Using range() Incorrectly: ...
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ 3 infinite iterators in python
3 Infinite Iterators in Python | Towards Data Science
January 21, 2025 - It is very common to use range() when we want to loop something for certain times. For example, for i in range(10) will loop 10 times.
๐ŸŒ
Study.com
study.com โ€บ computer science courses โ€บ computer science 113: programming in python
Infinite Loops in Python: Definition & Examples - Lesson | Study.com
July 18, 2024 - The code here produces the same result as the previous finite loop example using for: ... An infinite loop, on the other hand, is characterized by not having an explicit end, unlike the finite ones exemplified previously, where the control variable i clearly went from 0 to 9 (note that at the end i = 10, but that value of i wasn't printed). In an infinite loop the control is not explicitly clear, as in the example appearing here:
๐ŸŒ
Learning About Electronics
learningaboutelectronics.com โ€บ Articles โ€บ How-to-create-an-infinite-loop-in-Python.php
How to Create an Infinite Loop in Python
In this article, we show how to create an infinite loop in Python. An infinite loop that never ends; it never breaks out of the loop. So, whatever is in the loop gets executed forever, unless the program is terminated.