list comprehension question

Dave Angel d at davea.name
Wed Oct 17 08:32:43 EDT 2012


On 10/17/2012 12:43 AM, Kevin Anthony wrote:
> Is it not true that list comprehension is much faster the the for loops?
> 
> If it is not the correct way of doing this, i appoligize.
> Like i said, I'm learing list comprehension.
> 
(Please don't top-post;  it ruins the ordering.  In these forums, put
your response after the part you quote from earlier messages.  Or even
better, after each part you quote.  Then trim off the parts you didn't
reference.)

list comprehensions CAN be much faster, but not necessarily.  The most
complex a loop, the less likely it'll help much.

In any case, only the inner loop will be affected.  Nesting two list
comprehensions will make a trivial difference.

On the other hand, Hans Mulder shows some other factoring which seems
much more readable than yours.

Studying (and testing) those could teach you a lot about comprehensions,
as well as about the libraries that can help.  Note especially what
zip(*b) yields, and think about what it means.


-- 

DaveA



More information about the Python-list mailing list