getting the index while iterating through a list

Fernando Rodríguez fernandoSPAM.YOURSELF at fernando-rodriguez.com
Wed May 12 10:01:09 EDT 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



More information about the Python-list mailing list