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

Robert Kern rkern at ucsd.edu
Mon Apr 25 01:59:12 EDT 2005


Mike Meyer wrote:
> Ok, we've added list comprehensions to the language, and seen that
> they were good. We've added generator expressions to the language, and
> seen that they were good as well.
> 
> I'm left a bit confused, though - when would I use a list comp instead
> of a generator expression if I'm going to require 2.4 anyway?

Never. If you really need a list

list(x*x for x in xrange(10))

Sadly, we can't remove list comprehensions until 3.0.

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Python-list mailing list