Generator expressions v/s list comprehensions

Jeremy Bowers jerf at jerf.org
Sat Aug 28 16:47:19 EDT 2004


On Sat, 28 Aug 2004 14:09:34 -0600, Mahesh Padmanabhan wrote:
> Is returning a list really a limitation considering that lists can be 
> transformed quite easily?

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465

It is trivial to generate huge lists and more people need them than you
might think. Not choking the VM system with multi-hundred megabytes in
allocations is a non-trivial performance advantage, especially as the
alternative may be not finishing at all.

You, by the way, have it backwards. It is trivial to convert a generator
into a list, without advantage loss. The opposite is not possible. That's
why Alex, elsewhere in the newsgroup, is saying list comprehensions might
someday be replaced by list(x for x in whatever), but you're never going
to see him say generator expressions are adequately captured by iter([x
for x in whatever]).



More information about the Python-list mailing list