Better way to iterate over indices?
Benjamin Kaplan
benjamin.kaplan at case.edu
Tue Jun 21 14:20:41 EDT 2011
On Tue, Jun 21, 2011 at 11:05 AM, 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.
Why does enumerate seem clunky, other than the fact that you should
probably have
doStuff(i,v)
instead of myList[i] in there? It's a bit more awkward than C's
syntax, but since the typical use case is just iteration anyway, it's
not a huge deal for those few cases where you actually need the
indices.
>
> Are there any better ways to iterate over the indices of a list /tuple?
>
> --Bill
> --
> http://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list