[Python-3000] Adding sorting to generator comprehension

Ian Bicking ianb at colorstudy.com
Mon Apr 24 19:51:53 CEST 2006


Josiah Carlson wrote:
> One of the features of generator expressions which makes it desireable
> instead of list comprehensions is that generator expressions may use
> less memory *now*, and may be able to start returning results *now*.
> 
> Using (<genexp> orderby ...) as a replacement for sorted((genexp), key=...)
> is a bit misleading because while the original generator expression
> could have been space and time efficient, the orderby version certainly may
> not be.

Certainly it changes the performance substantially (of course if the 
expression is translated and executed elsewhere the performance can 
actually be improved, so it can go both ways).  Since list 
comprehensions are planned to just be syntactic sugar for generator 
comprehension, generator comprehensions are now the more fundamental 
construct.

But yeah, it is a little awkward, since something that is sorted can be 
returned as a list anyway, except for the fact that the expression 
itself could be ported off elsewhere which isn't normal Python.  (But 
should be normal Python!)

-- 
Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org


More information about the Python-3000 mailing list