In C, I would do this:
int i;
for (i = 0;; i++)
if (thereIsAReasonToBreak(i))
break;
How can I achieve something similar in Python?
In C, I would do this:
int i;
for (i = 0;; i++)
if (thereIsAReasonToBreak(i))
break;
How can I achieve something similar in Python?
Using
itertools.count
:In Python2
xrange()
is limited to sys.maxint, which may be enough for most practical purposes:In Python3,
range()
can go much higher, though not to infinity:So it's probably best to use
count()
.If you need to specify your preferred step size (like with range) and you would prefer not to have to import an external package (like itertools),
Probably the best solution to this is to use a while loop:
i=1 while True: if condition: break ...........rest code i+=1
This loop with go indefinitely.