Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > getting the index while iterating through a list

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
=?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

 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
=?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

 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking the available range while iterating through a string Ángel José Riesgo C++ 13 02-18-2011 09:10 AM
iterating through a block; declaring an index/counter within that block Thufir Ruby 1 10-20-2007 03:28 AM
Erasing in a vector while iterating through it =?iso-8859-1?q?Erik_Wikstr=F6m?= C++ 3 06-11-2007 04:10 PM
modifying a list while iterating through dustin.getz@gmail.com Python 4 02-26-2007 10:21 PM
Good practice? modifying vector while iterating through it Alan C++ 5 02-19-2007 05:28 AM



Advertisments