[Tutor] list comprehension, efficiency?

Bill Campbell bill at celestial.net
Tue Sep 28 19:12:19 CEST 2010

On Tue, Sep 28, 2010, Lie Ryan wrote:
>On 09/28/10 13:57, Bill Allen wrote:
>> I can now see that quite a bit of the code I write dealing with lists
>> can be done with list
>> comprehensions.   My question is this, is the list comprehension styled
>> code generally
>> more efficient at runtime?  If so, why?
>Yes, because the looping in list comprehension is done in C instead of a
>python construct. However, they are the type of efficiency that you
>shouldn't bother yourself with unless you're microoptimizing.

True enough, but dealing with large lists can be expensive,
particularly when appending.  Before I discovered sets, I found
significant time savings by creating a dictionary, adding as
necessary, then use d.keys() to get the keys back.

This became a significant factor when I was selecting unique
lines from about 1.3 million samples.

INTERNET:   bill at celestial.com  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:          (206) 236-1676  Mercer Island, WA 98040-0820
Fax:            (206) 232-9186  Skype: jwccsllc (206) 855-5792

Independent self-reliant people would be a counterproductive anachronism
in the collective society of the future where people will be defined by
their associations.  1896 John Dewey, educational philosopher, proponent
of modern public schools.

More information about the Tutor mailing list