shouldn't list comprehension be faster than for loops?

sturlamolden sturlamolden at yahoo.no
Fri Dec 18 13:44:29 EST 2009


On 17 Des, 18:37, Carlos Grohmann <carlos.grohm... at gmail.com> wrote:

> Tenting the time spent by each approach (using time.clock()), with a
> file with about 100,000 entries, I get 0.03s for the loop and 0.05s
> for the listcomp.
>
> thoughts?

Anything else being equal, list comprehensions will be the faster
becuase they incur fewer name and attribute lookups. It will be the
same as the difference between a for loop and a call to map. A list
comprehension is basically an enhancement of map.








More information about the Python-list mailing list