Generator expressions v/s list comprehensions
Martin DeMello
martindemello at yahoo.com
Sat Aug 28 16:40:58 EDT 2004
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?
Here's a simple example
linecount = sum(1 for line in file)
linecount = sum([1 for line in file])
The former requires an in-memory list equal to the size of the file.
Consigning "other than memory consumption" to a parenthetical remark
misses its importance.
martin
More information about the Python-list
mailing list