Some quick timing runs seem to give the 2nd option using range()
a slight edge over enumerate()
:
timeit a = [f(n) for n, _ in enumerate(mlist)]
10000 loops, best of 3: 118 us per loop
timeit a = [f(n) for n in range(len(mlist))]
10000 loops, best of 3: 102 us per loop
and just for fun using xrange()
(Python v2.7.2)
timeit a = [f(n) for n in xrange(len(mlist))]
10000 loops, best of 3: 99 us per loop
I would favor readable code first, then using xrange()
if available (i.e., Pre-Python v 3.x), followed by range()
and enumerate()
.