Generator expressions v/s list comprehensions

Mahesh Padmanabhan mahesh at privacy.net
Sat Aug 28 20:19:55 CEST 2004


Hi,

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. 

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?

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?

Thanks,
Mahesh



More information about the Python-list mailing list