getting the index while iterating through a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fernando Rodríguez

    getting the index while iterating through a list

    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
  • Holger Türk

    #2
    Re: getting the index while iterating through a list



    Fernando Rodríguez wrote:[color=blue]
    > 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()?[/color]

    Not with map. Use zip:

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

    Greetings,

    Holger

    Comment

    • Peter Otten

      #3
      Re: getting the index while iterating through a list

      Fernando Rodríguez wrote:
      [color=blue]
      > 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()?[/color]
      [color=blue][color=green][color=darkred]
      >>> aList = ["alpha", "beta", "gamma"]
      >>> list(enumerate( aList))[/color][/color][/color]
      [(0, 'alpha'), (1, 'beta'), (2, 'gamma')][color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      Peter

      Comment

      • François Pinard

        #4
        Re: getting the index while iterating through a list

        [Fernando Rodríguez][color=blue]
        > 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:[/color]
        [color=blue]
        > i = 0
        > newList = []
        > for element in aList:
        > newList.append( (i, element))
        > i += 1[/color]
        [color=blue]
        > Is there a more elegant way of doing this with for? And with map()?[/color]

        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

        Comment

        • Steven Rumbalski

          #5
          Re: getting the index while iterating through a list

          Fernando Rodríguez wrote:
          [color=blue]
          > 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[/color]

          Try enumerate:[color=blue][color=green][color=darkred]
          >>> newList = [(i, element) for i, element in enumerate(aList )][/color][/color][/color]

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

          enumerate(itera ble)
          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|rumbals ki|dot|com

          Comment

          Working...