enumerated while loop

Terry Reedy tjreedy at udel.edu
Sat Jan 23 18:34:53 EST 2010


On 1/23/2010 9:44 AM, Roald de Vries wrote:
> Dear all,
>
> I sometimes want to use an infinite while loop with access to the loop
> index, like this:
>
> def naturals():
> i = 0
> while True:
> yield i
> y += 1
>
> for i in naturals():
> print(i)
>
> I assume a function like 'naturals' already exists, or a similar
> construction for the same purpose. But what is it called?

itertools.count




More information about the Python-list mailing list