Generator expressions v/s list comprehensions

Mel Wilson mwilson at the-wire.com
Sun Aug 29 02:00:45 CEST 2004


In article <mahesh-7E265C.14093428082004 at individual.net>,
Mahesh Padmanabhan <mahesh at privacy.net> wrote:
>I am still not clear of the advantages of using generator expressions
>(other than less memory consumption) instead of list comprehension for
>any given class of problems. Can you cite concrete use cases where
>generator expressions would be preferred over list comprehension?

   Storage economy and the time saved by omitting lots of
object creation and destruction are the main advantages of
generator expressions.

   The advantage of lists is that they'll stand still while
you do things to them (things like iterating over them in
many and novel ways.)  The mind does boggle at the idea of a
data-processing program that has replaced all containers
with generator expressions, and creates all its information
on-demand without storing anything anywhere.

        Regards.        Mel.



More information about the Python-list mailing list