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

jfj jfj at freemail.gr
Mon Apr 25 09:30:05 EDT 2005


Mike Meyer wrote:

> jfj <jfj at freemail.gr> writes:
> 
> 
>>I think a better question would be "What do *generator expressions* do
>>that list comprehensions don't?".  And always use list comprehensions
>>unless you want the extra bit.
> 
> 
> As the OP, I can say why I didn't ask those questions.

Sorry. I was referring to the subject line:)

> 
> Generator expressions don't build the entire list in memory before you
> have to deal with it. This makes it possible to deal with expressions
> that are to long to fit in memory.
> 
> Which means that the real rule should be always use generator
> expressions, unless you *know* the expression will always fit in
> memory.
>

Consider this code which I also included the first reply:

    x = [i for in in something()]
    random.shuffle (x)
    x.sort ()

Shuffle and sort are two examples where need *the entire list* to
work. Similarily for a dictionary where the values are small lists.
In this example using a generator buys you *nothing* because you
will immediately build a list.

So there are cases where we need the list as the product of an algorithm
and a generator is not good enough.  In fact, in my experience with 
python so far I'd say that those cases are the most common case.

That is the one question.
The other question is "why not list(generator) instead of [list 
comprehension]?"

I guess that lists are *so important* that having a primary language
feature for building them is worth it.  On the other hand "list()" is
not a primary operator of the python language. It is merely a builtin
function.


jfj




More information about the Python-list mailing list