Make a for loop repeat forever

for loops are useful in python in many ways, but one of the most common usages associated with a for loop is it's ability to repeat a certain amount of times. Like this:

for times in range(50):
print "I'm repeating this 50 times..."


The challenge I propose to you is to make a for repeat forever. If you can find a way to do it without using a recursion, kudos to you. You can use any major version of Python. Just specify which one you are using. The goal is to do this in the smallest file size you can get, and a method that won't "break" after a while. Good luck!

• Ewwww, language-specific challenges. (See meta.codegolf.stackexchange.com/a/234/3 for my stance on this.) (This is unrelated to the reason why I closed the question, which is a site rule and not just a personal opinion.) Jun 20, 2014 at 13:18
• @ChrisJester-Young I personally think it's fair for programming-puzzles, as they usually tend to work around some feature of a specific language. You could have told the OP though, that your closing the question has nothing to do with your not liking language-specific challenges. So, @ Some Guy, the question is closed because you did not indicate who (among several correct solutions) you will pick the winner of the challenge. Votes? Code-length? Something else? Jun 20, 2014 at 13:32
• @m.buettner There, I edited my comment to say that now. Thanks! :-) Jun 20, 2014 at 13:35
• Here's an idea to make this non-language-specific: "implement a foreach loop that repeats forever" Jun 20, 2014 at 16:00
• @TheRare Changing the accepted answer is absolutely legitimate if a new winner comes along (by the specified criteria). Therefore, the currently accepted answer isn't even the correct one, actually. Jun 20, 2014 at 18:36

This can be done quite easily by abusing floating point addition.

def frange(start, stop, step):
r = start
while r < stop:
yield r
r += step

for x in frange(0, 1e16, 1):
print (x)


IEEE754 64-bit floats have 53 bits of mantissa (including the implicit bit), so if x > 1<<53 then x + 1 == x. 1 << 53 is approximately 9e15.

• Are you sure this won't transform to a long? Jun 20, 2014 at 13:18
• @TheRare, why would it transform to a long? That aside, a simple test shows arithmetic behaving as expected. Jun 20, 2014 at 13:23
• That is actually pretty interesting. Jun 20, 2014 at 13:24

Because this was turned to a codegolf, have a 48 bytes version as a one-liner. 3 bytes could be removed from both by replacing print i with pass.

for i in __import__('itertools').count():print i


Or a 47 bytes version with two lines.

from itertools import*
for i in count():print i


Old message:

Easy it is.

def f(i = 0):
while True:
yield i
i += 1
for i in f():
print(i)


This is an interesting way of doing it (32 chars for class x, 46 not counting the print):

class x:__iter__=next=lambda s:s
for i in x():print "Hello"


This defines an iterable whose next method never raises a StopIteration

• Actually, the next method is the __iter__ method (which is supposed to return the iterable). Both return self (s) Jun 22, 2014 at 5:13

I don't usually golf in Python, probably some reason people haven't already done posted this 23-character solution.

x=[1]
for i in x:
x+=x


Much less memory-hungry if you do x+=[i] instead. Either one hangs my 2.7.5

• "and a method that won't "break" after a while." So that's why no one has done this. Or maybe it's because no one was smart enough to think of it. Jun 20, 2014 at 16:28
• Well, this does explode the memory very quickly, because the memory usage raises exponentially. Jun 20, 2014 at 18:56
• Yeah, the +=[i] version seems almost legitimate though, I don't know if that would break within any reasonable amount of time. Either way, not as interesting a solution as the others. Jun 20, 2014 at 21:12
• It would probably break eventually, when the list reaches sys.maxsize items.
– Luna
Jul 5, 2014 at 12:11
for i in iter(lambda : 0, 1):
print (i) # prints 0 forever


The iter function is normally used to create an iterator from an iterable.

The iter function can also take two arguments, a callable and a sentinel value. In that case it returns an iterator that yields the result of the callable until the callable returns the sentinel value.

In this case, the callable is a lambda that always returns 0. Since it never can return the sentinel value (1), this will loop forever.

Since this is code golf, this version is 28 characters and only one line:

for i in iter(lambda:0,1):i


28 bytes, infinite, and no itertools

for x in iter(int,1):print 1


Shouldn't end without system program cutoffs (i.e. not my fault).

22 bytes without printing anything:

for x in iter(int,1):1


39 Characters, no itertools and no crashing

def f():
while 1:yield
for i in f():1


Itertools is unnecessary, you can define your own generator.

• You don't need to use pass - 1 or i will do just as well. Jul 4, 2014 at 22:16

Python 2 & 3: 37 characters

class P:__getitem__=id
for t in P():1


¡No infinite memory required!

Itertools, to the rescue:

from itertools import count
for i in count():
print i


or

from itertools import repeat
for i in repeat(42):
print i

• Just an fyi, itertools.count is the same as my answer and itertools.repeat is almost the same as my original answer. Jun 20, 2014 at 13:13
• @TheRare I suppose, but I'd rather use itertools.count in an production environment than something home-brew. Jun 20, 2014 at 13:15
• Absolutely. Just wanted to inform that the implementation is the same. Jun 20, 2014 at 13:16

Python 3.3: 2 lines, 29 characters

j=[1]
for i in j: j.append(1)


Does this break? I've run it for a short while, doesn't seem to hang.

• This will run out of memory, just like histocrat's version. Jun 21, 2014 at 14:31
• @m.buettner indeed it will. Hmm. Jun 21, 2014 at 14:34