Getting number of iteration

Mike Meyer mwm at mired.org
Fri May 6 16:16:37 EDT 2005


Bill Mill <bill.mill at gmail.com> writes:

> 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?
> Earlier:
>
> n = 0
> for x in lst:
>     print "iteration %d on element %s" % (n, x)
>     n += 1

Just for the record, the old idiom was:

for n in xrange(len(lst)):
    x = lst[n]
    print "iteration %d on element %s" % (n, x)

    <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list