You can use the second argument of iter(), to call a function repeatedly until its return value matches that argument. This would loop forever as 1 will never be equal to 0 (which is the return value of int()):

for _ in iter(int, 1):
    pass

If you wanted an infinite loop using numbers that are incrementing you could use itertools.count:

from itertools import count

for i in count(0):
    ....
Answer from Padraic Cunningham on Stack Overflow
🌐
The Python Coding Stack
thepythoncodingstack.com › p › infinite-for-loop-infinite-iterator-python
To Infinity and Beyond • The Infinite `for` Loop
March 7, 2024 - You can import itertools and update ... an infinite animation. The for loop iterates through all_positions, but all_positions is an infinite iterator....
Discussions

[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
loops - Looping from 1 to infinity in Python - Stack Overflow
In Python2 range creates and returns an actual list of ints. You won't have enough RAM for such a big list 2017-02-13T20:48:05.293Z+00:00 ... def to_infinity(): index = 0 while True: yield index index += 1 for i in to_infinity(): if i > 10: break ... But beware, modifying i will not affect the flow of the loop ... More on stackoverflow.com
🌐 stackoverflow.com
A problem with an infinite for-loop in python interacting with a list of 3 values
In your original code you're modifying a list while looping over it. Never do that, the behavior is always unpredictable as you've found out here. The reason why looping over words[:] works is because that syntax creates a shallow copy to be looped over and you modify the original list. words[:] is essentially equivalent to words.copy() which is what you should likely be doing here. Here's a blog post which can explain some more of this More on reddit.com
🌐 r/learnpython
11
6
October 17, 2019
Infinite for loop
i am using a set of for loops to update directories to all of their directories, but it has entered a infinite loop and i dont know why. Attr7 = Attr4 Attr4 = [] print("3") for Attr8 in Attr7: print("new") for A… More on discuss.python.org
🌐 discuss.python.org
0
0
June 27, 2021
🌐
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 ...
🌐
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.

🌐
Runestone Academy
runestone.academy › ns › books › published › py4e-int › iterations › infinite_loops.html
6.3. Infinite loops — Python for Everybody - Interactive
This loop is obviously an infinite loop because the logical expression on the while statement is simply the logical constant True: ... As you can see above, the Code Lens gives you a warning because it runs for over 1000 steps. If you make the mistake of running this code, you will learn quickly how to stop a runaway Python ...
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
March 3, 2025 - In this tutorial, you'll learn about indefinite iteration using the Python while loop. You'll be able to construct basic and complex while loops, interrupt loop execution with break and continue, use the else clause with a while loop, and deal with infinite loops.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
If we want a block of code to execute infinite number of times then we can use the while loop in Python to do so. Code given below uses a 'while' loop with the condition "True", which means that the loop will run infinitely until we break out ...
Published   2 days ago
🌐
Reddit
reddit.com › r/learnpython › a problem with an infinite for-loop in python interacting with a list of 3 values
r/learnpython on Reddit: A problem with an infinite for-loop in python interacting with a list of 3 values
October 17, 2019 -
words = ['cat', 'window', 'defenestrate']

for w in words:
     if len(w) > 6:
         words.insert(0, w)

With for w in words:, the example would attempt to create an infinite list, inserting defenestrate over and over again. I don't understand why the for-loop does it; shouldn't it exit from the loop when it comes to the value defenestrate and the code below the if statement is executed since it evaluates to True.

If the statement was while without any break statement I would understand, but with a for-loop I don't understand. Why is there no end when it doesn't have to go through an infinite list but only a list with 3 values.

In contrast, If I just change the line for w in words: to for w in words[:]: the loop will no longer be infinite and the output will be ['defenestrate', 'cat', 'window', 'defenestrate'] Why is that so?

I am a beginner in programming and would be grateful if you could answer with no advanced terms. Thx.

The example itself can be seen in the Python documentation 3.7.2 under Tutorials, under More Control Flow Tools, under For Statements.

🌐
Scaler
scaler.com › home › topics › what is infinite loop in python?
What is Infinite Loop in Python? | Scaler Topics
May 8, 2024 - We have mainly three types of infinite loops in Python. They are: ... Let us discuss them one by one briefly. A fake infinite loop is a kind of loop that looks like an infinite loop (like while True: or while 1:) but has some certain condition that will terminate the loop. For example, we are given a program in which we need to print all the even multiples of 3 that is less than and equal to a given number n.
🌐
Python.org
discuss.python.org › python help
Infinite for loop - Python Help - Discussions on Python.org
June 27, 2021 - i am using a set of for loops to update directories to all of their directories, but it has entered a infinite loop and i dont know why. Attr7 = Attr4 Attr4 = [] print("3") for Attr8 in Attr7: print("new") for Attr9 in dir(Key1 + "." + Attr8): print(Attr9) print("5") Attr4.append(Attr8 + "." + Attr9) print("end") if i enter a single directory it prints “new” every cycle but never prints “end”.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › python infinite loop
Python Infinite Loop | Top 4 Types of Statements in Python Infinite Loop
May 10, 2024 - Loops are compelling and essential, but an infinite loop is the only pitfall. Python has two types of loops, only ‘While loop’ and ‘For loop.’ While loop works precisely as the IF statement but in the IF statement, we run the block of code just once, whereas, in a while loop, we jump ...
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Unstop
unstop.com › home › blog › python infinite loop | types, applications & more (+examples)
Python Infinite Loop | Types, Applications & More (+Examples)
October 25, 2024 - Initiate the Loop: Use the while True: statement to start it. This means the loop will run indefinitely until a break condition is met. while True: # Code to execute repeatedly print("This loop will run forever")
🌐
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 ...
🌐
StudySmarter
studysmarter.co.uk › computer science › computer programming › python infinite loop
Python Infinite Loop: Definition & Examples
August 10, 2023 - This comprehensive guide will help you understand what Python Infinite Loops are, their purpose, and the benefits and drawbacks of using them. Additionally, you will learn how to create basic infinite loop examples using 'while True' and explore various methods, such as the 'for loop' structure and incorporating 'time.sleep()' in your code.
🌐
Programiz
programiz.com › python-programming › looping-technique
Python Looping Techniques
# 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) ... Enter an integer: 3 The double of 3 is 6 Enter an integer: 5 The double of 5 is 10 Enter an integer: 6 The double of 6 is 12 Enter an integer: Traceback (most recent call last): This is a normal while loop without break statements.
🌐
Python.org
discuss.python.org › python help
Infinte while loop using try/except - Python Help - Discussions on Python.org
June 15, 2024 - hey y’all! I’m taking an online class called Cs50P, I thought I practiced using while loops + try/exceptions to the point of being confident using this structure. I’m running into an infinite loop when the exceptions are raised in the convert() function.
🌐
TutorialsPoint
tutorialspoint.com › how-to-prevent-loops-going-into-infinite-mode-in-python
How to prevent loops going into infinite mode in Python?
August 23, 2023 - In python the while loop needs ... beginning to false. This is usually done by keeping count of iterations. If the while loop condition never evaluates to False, then we will have an infinite loop, which is a loop that never stops automatically, in this case we need to interrupt ...
🌐
Scientech Easy
scientecheasy.com › home › blog › infinite loop in python
Infinite Loop in Python - Scientech Easy
January 25, 2026 - Learn infinite loop in Python with example, how to create it using while loop statement, how to stop an infinite loop using break statement
🌐
Better Programming
betterprogramming.pub › over-engineering-the-infinite-loop-in-python-53450cb52132
Over-Engineering the Infinite Loop in Python | by Nicholas Obert | Better Programming
September 20, 2023 - The most Pythonic approach to an infinite loop is to use a while loop with a constantly-true literal value simply: True. Very straightforward, isn't it? But it’s also boring at the same time.