Better way to iterate over indices?

Noah Hall enalicho at gmail.com
Tue Jun 21 14:19:30 EDT 2011


On Tue, Jun 21, 2011 at 7:05 PM, Billy Mays <noway at nohow.com> wrote:
> I have always found that iterating over the indices of a list/tuple is not
> very clean:
>> for i in range(len(myList)):
>    doStuff(i, myList[i])

> I know I could use enumerate:
>> for i, v in enumerate(myList):
>    doStuff(i, myList[i])
>> ...but that stiff seems clunky.

You're not using it properly. Think about it. You're giving two names
- i and v. You've forgotten about v -
>>> for i, v in enumerate('fish'):
... print i, v
...
0 f
1 i
2 s
3 h
HTH.


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /