What's do list comprehensions do that generator expressions don't?

Jeremy Bowers jerf at jerf.org
Mon Apr 25 15:29:03 EDT 2005


On Sun, 24 Apr 2005 22:59:12 -0700, Robert Kern wrote:
> Never. If you really need a list
> 
> list(x*x for x in xrange(10))
> 
> Sadly, we can't remove list comprehensions until 3.0.

Why "remove" them? Instead, we have these things called "comprehensions"
(which, now that I say that, seems a rather odd name), and you can control
whether they result in a list or a generator with () or [].

I don't see why they need to be "removed". Lists are already a special
case of the "only one way to do it" principle ([] vs. list()), and
pragmatically I don't see any reason to remove them here; it doesn't add
comprehensibility, leaving them in doesn't significantly affect the mental
size of the code (the *comprehension* is the hard part, the final form
should be relatively simple), it's not worth breaking that code.



More information about the Python-list mailing list