getting the index while iterating through a list

  • Thread starter Fernando Rodríguez
  • Start date
F

Fernando Rodríguez

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?=

Fernando said:
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
 
P

Peter Otten

Fernando said:
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

[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))
 
S

Steven Rumbalski

Fernando said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top