Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify a generator vs list comprehension

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?

like image 799
Ankur Agarwal Avatar asked Aug 13 '13 14:08

Ankur Agarwal


People also ask

What makes a list comprehension different from a generator?

So what's the difference between Generator Expressions and List Comprehensions? The generator yields one item at a time and generates item only when in demand. Whereas, in a list comprehension, Python reserves memory for the whole list.

When should you use a generator instead of a list?

The generator yields one item at a time — thus it is more memory efficient than a list. For example, when you want to iterate over a list, Python reserves memory for the whole list. A generator won't keep the whole sequence in memory, and will only “generate” the next element of the sequence on demand.

What is a generator comprehension?

A generator comprehension is a single-line specification for defining a generator in Python. It is absolutely essential to learn this syntax in order to write simple and readable code. Note: Generator comprehensions are not the only method for defining generators in Python.

What is the difference between list and list comprehension?

The for loop is a common way to iterate through a list. List comprehension, on the other hand, is a more efficient way to iterate through a list because it requires fewer lines of code. List comprehension requires less computation power than a for loop because it takes up less space and code.


2 Answers

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.

like image 148
Martijn Pieters Avatar answered Oct 28 '22 11:10

Martijn Pieters


List comprehensions are enclosed in []:

>>> [i*i for i in xrange(5)]  # list comprehension
[0, 1, 4, 9, 16]
>>> (i*i for i in xrange(5))  # generator
<generator object <genexpr> at 0x2cee40>

You are passing a generator.

like image 29
arshajii Avatar answered Oct 28 '22 10:10

arshajii