getting the index while iterating through a list

Discussion in 'Python' started by Fernando Rodríguez, May 12, 2004.

  1. 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
     
    Fernando Rodríguez, May 12, 2004
    #1
    1. Advertising

  2. 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
     
    =?ISO-8859-1?Q?Holger_T=FCrk?=, May 12, 2004
    #2
    1. Advertising

  3. Fernando Rodríguez

    Peter Otten Guest

    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
     
    Peter Otten, May 12, 2004
    #3
  4. [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
     
    =?iso-8859-1?Q?Fran=E7ois?= Pinard, May 12, 2004
    #4
  5. 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
     
    Steven Rumbalski, May 12, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    404
    Steven D'Aprano
    Feb 26, 2007
  2. Alan
    Replies:
    5
    Views:
    1,070
    Grizlyk
    Feb 19, 2007
  3. =?iso-8859-1?q?Erik_Wikstr=F6m?=

    Erasing in a vector while iterating through it

    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jun 11, 2007, in forum: C++
    Replies:
    3
    Views:
    816
    Christopher Dearlove
    Jun 11, 2007
  4. Ángel José Riesgo
    Replies:
    13
    Views:
    505
    Ángel José Riesgo
    Feb 18, 2011
  5. Thufir
    Replies:
    1
    Views:
    144
    7stud --
    Oct 20, 2007
Loading...

Share This Page