Python – Identify Generator vs List Comprehension

generatorgenerator-expressionlist-comprehensionpython

I have this:

>>> sum( i*i for i in xrange(5))

My question is, in this case am I passing a list comprehension or a generator object to sum ? How do I tell that? Is there a general rule around this?

Also remember sum by itself needs a pair of parentheses to surround its arguments. I'd think that the parentheses above are for sum and not for creating a generator object. Wouldn't you agree?

Best Answer

You are passing in a generator expression.

A list comprehension is specified with square brackets ([...]). A list comprehension builds a list object first, so it uses syntax closely related to the list literal syntax:

list_literal = [1, 2, 3]
list_comprehension = [i for i in range(4) if i > 0]

A generator expression, on the other hand, creates an iterator object. Only when iterating over that object is the contained loop executed and are items produced. The generator expression does not retain those items; there is no list object being built.

A generator expression always uses (...) round parethesis, but when used as the only argument to a call, the parenthesis can be omitted; the following two expressions are equivalent:

sum((i*i for i in xrange(5)))  # with parenthesis
sum(i*i for i in xrange(5))    # without parenthesis around the generator

Quoting from the generator expression documentation:

The parentheses can be omitted on calls with only one argument. See section Calls for the detail.