Velocity Reviews > getting the index while iterating through a list

# getting the index while iterating through a list

Fernando Rodríguez
Guest
Posts: n/a

 05-12-2004
Hi,

While iterating through a list I'd like to know not just the current element,
but also its index. Is there a better way than this:

i = 0
newList = []
for element in aList:
newList.append((i, element))
i += 1

Is there a more elegant way of doing this with for? And with map()?

Thanks

=?ISO-8859-1?Q?Holger_T=FCrk?=
Guest
Posts: n/a

 05-12-2004

Fernando Rodríguez wrote:
> While iterating through a list I'd like to know not just the current element,
> but also its index. Is there a better way than this:
>
> i = 0
> newList = []
> for element in aList:
> newList.append((i, element))
> i += 1
>
> Is there a more elegant way of doing this with for? And with map()?

Not with map. Use zip:

newList = zip (range (len (aList)), aList)

Greetings,

Holger

Peter Otten
Guest
Posts: n/a

 05-12-2004
Fernando Rodríguez wrote:

> While iterating through a list I'd like to know not just the current
> element, but also its index. Is there a better way than this:
>
> i = 0
> newList = []
> for element in aList:
> newList.append((i, element))
> i += 1
>
> Is there a more elegant way of doing this with for? And with map()?

>>> aList = ["alpha", "beta", "gamma"]
>>> list(enumerate(aList))

[(0, 'alpha'), (1, 'beta'), (2, 'gamma')]
>>>

Peter

=?iso-8859-1?Q?Fran=E7ois?= Pinard
Guest
Posts: n/a

 05-12-2004
[Fernando Rodríguez]
> While iterating through a list I'd like to know not just the current element,
> but also its index. Is there a better way than this:

> i = 0
> newList = []
> for element in aList:
> newList.append((i, element))
> i += 1

> Is there a more elegant way of doing this with for? And with map()?

Hi, Fernando. You may write something like:

newList = []
for i, element in enumerate(aList):
newList.append((i, element))

or even simpler:

newList = list(enumerate(aList))

--
François Pinard http://www.iro.umontreal.ca/~pinard

Steven Rumbalski
Guest
Posts: n/a

 05-12-2004
Fernando Rodríguez wrote:

> Hi,
>
> While iterating through a list I'd like to know not just the current
> element, but also its index. Is there a better way than this:
>
> i = 0
> newList = []
> for element in aList:
> newList.append((i, element))
> i += 1
>
> Is there a more elegant way of doing this with for? And with map()?
>
> Thanks

Try enumerate:
>>> newList = [(i, element) for i, element in enumerate(aList)]

from Python-Docs-2.3/lib/built-in-funcs.html:

enumerate(iterable)
Return an enumerate object. iterable must be a sequence, an iterator, or
some other object which supports iteration. The next() method of the
iterator returned by enumerate() returns a tuple containing a count
(from zero) and the corresponding value obtained from iterating over
iterable. enumerate() is useful for obtaining an indexed series: (0,
seq[0]), (1, seq[1]), (2, seq[2]), .... New in version 2.3.

--
Steven Rumbalski
news|at|rumbalski|dot|com