tags:

views:

1726

answers:

3

I could swear I've seen the function (or method) that takes a list, like this [3,7,19] and makes it into iterable list of tuples, like so: [ (0,3),(1,7),(2,19) ] to use it instead of

for i in range(len(name_of_list)):
    name_of_list[i]=something

but I can't remember the name and "iterate list" in Google gets nothing.

+24  A: 
>>> a = [3,4,5,6]
>>> for id,val in enumerate(a):
...   print id,val
...
0 3
1 4
2 5
3 6
>>>
Vinko Vrsalovic
+18  A: 

Yep that would be the enumerate function ! Or more to the point you need to do:

list(enumerate([3,7,19]))

[(0, 3), (1, 7), (2, 19)]
PierreBdR
I don't know why this isn't the accepted answer...
Beau Martínez
+4  A: 

Here's another using the zip function.

>>> a=  [3,7,19]
>>> zip( range(len(a)), a )
[(0, 3), (1, 7), (2, 19)]
S.Lott
`enumerate()` is a bit more elegant, I think.
Nathan Fellman