enumerated while loop
Roald de Vries
rdv at roalddevries.nl
Sat Jan 23 10:07:51 EST 2010
On Jan 23, 2010, at 3:49 PM, Diez B. Roggisch wrote:
> Am 23.01.10 15:44, schrieb Roald de Vries:
>> 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?
>
> for i, item in enumerate(iterable):
> ....
This only moves the problem to the variable 'iterable'. And moreover
is more complex than needed; it give '(i, item)', whereas I only need
the index 'i'.
More information about the Python-list
mailing list