Generator expressions v/s list comprehensions

Nicolas Lehuen nicolas at lehuen.com
Wed Sep 1 10:36:57 CEST 2004


python at rcn.com (Raymond Hettinger) wrote in message news:<5d83790c.0408311434.3cc38c24 at posting.google.com>...
> [Mahesh Padmanabhan]
> > Now I see that generator expressions have been added to the language 
> > with 2.4 and I question the need for it. I know that it allows for lazy 
> > evaluation which speeds things up for larger lists but why was it 
> > necessary to add it instead of improving list comprehension? 
> 
> FWIW, we improved list comprehensions too.  They run much faster now.
> 
> 
> Raymond

IIRC, list constructors and list comprehensions now use the length of
the underlying sequence (if it defines __len__) to preallocate its
internal array.

Thus, the list comprehension is theoritically more efficient in this
case :

[x for x in range(100000)] vs [x for x in xrange(100000)]

So [x for x in whatever] may be preferable to list(x for x in
whatever) if you don't know what 'whatever' is and it happens in
define __len__.

BTW, I guess this is no longer true with an if statement :

[x for x in range(100000) if x%2==0] may be less efficient than [x for
x in xrange(100000) if x%2==0]

Regards,

Nicolas Lehuen



More information about the Python-list mailing list