Get number of iteration

Sidharth Kuruvila sidharthk at hotmail.com
Thu Jan 29 11:41:39 EST 2004


there is a function in python 2.3 called enumerate.


>>> l = range(0,51,5)
>>> e = enumerate(range(0,51,5))
>>> e
<enumerate object at 0x0119BBC0>
>>> list(e)
[(0, 0), (1, 5), (2, 10), (3, 15), (4, 20), (5, 25), (6, 30), (7, 35), (8,
40), (9, 45), (10, 50)]
>>> e = enumerate(range(0,51,5))
>>> for index, value in e:
...  print "The index of value %i is %i" % (value, index)
...
The index of value 0 is 0
The index of value 5 is 1
The index of value 10 is 2
.
.
.

if you are using an older version of python which doesn't have enumerate
you could do this
>>> l = range(0,51,5)
>>> zip(range(len(l)), l)
[(0, 0), (1, 5), (2, 10), (3, 15), (4, 20), (5, 25), (6, 30), (7, 35), (8,
40), (9, 45), (10, 50)]






More information about the Python-list mailing list