Generator expressions v/s list comprehensions
apardon at forel.vub.ac.be
Tue Aug 31 14:14:33 CEST 2004
Op 2004-08-28, Alex Martelli schreef <aleaxit at yahoo.com>:
> Mahesh Padmanabhan <mahesh at privacy.net> wrote:
>> When list comprehension was added to the language, I had a lot of
>> trouble understanding it but now that I am familiar with it, I am not
>> sure how I programmed in Python without it.
> Oh good.
>> 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?
>> Was there some sort of limitation that prevented list comprehension from
>> taking over the functionality that generator expressions provide?
> Sure, and that limitation is: list comprehensions return lists. This
> one "limitation" (together with Python's applicative order evaluation,
> and you couldn't change THAT without breaking the whole caboodle of
> existing programs!) implies everything else.
>> I have always liked the fact that Python has limited capabilities for
>> having more than one way to do it and it seem to me that generator
>> expressions break that philosophy. It is similar to how you have to use
>> range or xrange depending on how large the range is.
>> Can someone please explain the reasoning behind it?
> Generator comprehensions are wonderful and there is no way Python list
> comprehensions can provide the same features, since lists need to be
> lists. Sure, list(e(x) for x in foo) IS just the same thing as [e(x)
> for x in foo]. We'll remove the redundancy in 3.0 -- not earlier
> because it will break backwards compatibility. The only sensible way I
> can see right now for 3.0 to remove this redundancy is by removing list
> comprehensions and leaving only generator comprehensions, btw.
The one problem I have with generator comprehensions is that, because
list comprhension came first they look like tuple comprehensions.
In order to avoid this same kind of confusion with new comers I think
it would be best if generator comprehensions are explained first and
that list comprehension is just syntax sugar for list(genexp)
More information about the Python-list