Is it possible to get an infinite loop in for loop?
My guess is that there can be an infinite for loop in Python. I\'d like to know this for future refe
Yes, use a generator that always yields another number: Here is an example
def zero_to_infinity():
i = 0
while True:
yield i
i += 1
for x in zero_to_infinity():
print(x)
It is also possible to achieve this by mutating the list you're iterating on, for example:
l = [1]
for x in l:
l.append(x + 1)
print(x)