On 5/6/05, Florian Lindner <[EMAIL PROTECTED]> 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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to