Getting number of iteration

Bill Mill bill.mill at gmail.com
Fri May 6 12:06:31 EDT 2005


On 5/6/05, Bill Mill <bill.mill at gmail.com> wrote:
> 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:

ummm, make that 2.3 and greater. I always think things are more recent
than they are.

> 
> 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