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

Mike Meyer mwm at mired.org
Mon Apr 25 09:51:17 EDT 2005


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.

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.

Which leads to the obvious question of why the exception.

      <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list