Getting number of iteration
Bill Mill
bill.mill at gmail.com
Fri May 6 12:04:28 EDT 2005
On 5/6/05, Florian Lindner <Florian.Lindner at xgm.de> wrote:
> Hello,
> when I'm iterating through a list with:
>
> for x in list:
>
> how can I get the number of the current iteration?
Python 2.4 and greater:
for n, x in enumerate(lst):
print "iteration %d on element %s" % (n, x)
Earlier:
n = 0
for x in lst:
print "iteration %d on element %s" % (n, x)
n += 1
And you shouldn't use list as a variable name; list() is a built-in
function which you'll clobber if you do.
Peace
Bill Mill
bill.mill at gmail.com
More information about the Python-list
mailing list