Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8107

Re: Better way to iterate over indices?

References <itqmkb$h56$1@speranza.aioe.org>
From Noah Hall <enalicho@gmail.com>
Date 2011-06-21 19:19 +0100
Subject Re: Better way to iterate over indices?
Newsgroups comp.lang.python
Message-ID <mailman.233.1308680392.1164.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Jun 21, 2011 at 7:05 PM, Billy Mays <noway@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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Better way to iterate over indices? Billy Mays <noway@nohow.com> - 2011-06-21 14:05 -0400
  Re: Better way to iterate over indices? Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-21 12:15 -0600
  Re: Better way to iterate over indices? Noah Hall <enalicho@gmail.com> - 2011-06-21 19:19 +0100
  Re: Better way to iterate over indices? Benjamin Kaplan <benjamin.kaplan@case.edu> - 2011-06-21 11:20 -0700
  Re: Better way to iterate over indices? Ethan Furman <ethan@stoneleaf.us> - 2011-06-21 11:35 -0700

csiph-web