Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly does a generator comprehension work?

Tags:

python

What does generator comprehension do? How does it work? I couldn't find a tutorial about it.

like image 620
NONEenglisher Avatar asked Dec 13 '08 03:12

NONEenglisher


People also ask

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 a generator and comprehension?

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. Thus we can say that the generator expressions are memory efficient than the lists.

What are generator iterators list comprehension in Python?

Generators are special iterators in Python which returns the generator object. The point of using it, is to generate a sequence of items without having to store them in memory and this is why you can use Generator only once.

What is generator expression in Python?

A generator expression is an expression that returns a generator object. Basically, a generator function is a function that contains a yield statement and returns a generator object. For example, the following defines a generator function: def squares(length): for n in range(length): yield n ** 2.


2 Answers

Do you understand list comprehensions? If so, a generator expression is like a list comprehension, but instead of finding all the items you're interested and packing them into list, it waits, and yields each item out of the expression, one by one.

>>> my_list = [1, 3, 5, 9, 2, 6] >>> filtered_list = [item for item in my_list if item > 3] >>> print(filtered_list) [5, 9, 6] >>> len(filtered_list) 3 >>> # compare to generator expression ...  >>> filtered_gen = (item for item in my_list if item > 3) >>> print(filtered_gen)  # notice it's a generator object <generator object <genexpr> at 0x7f2ad75f89e0> >>> len(filtered_gen) # So technically, it has no length Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: object of type 'generator' has no len() >>> # We extract each item out individually. We'll do it manually first. ...  >>> next(filtered_gen) 5 >>> next(filtered_gen) 9 >>> next(filtered_gen) 6 >>> next(filtered_gen) # Should be all out of items and give an error Traceback (most recent call last):   File "<stdin>", line 1, in <module> StopIteration >>> # Yup, the generator is spent. No values for you! ...  >>> # Let's prove it gives the same results as our list comprehension ...  >>> filtered_gen = (item for item in my_list if item > 3) >>> gen_to_list = list(filtered_gen) >>> print(gen_to_list) [5, 9, 6] >>> filtered_list == gen_to_list True >>>  

Because a generator expression only has to yield one item at a time, it can lead to big savings in memory usage. Generator expressions make the most sense in scenarios where you need to take one item at a time, do a lot of calculations based on that item, and then move on to the next item. If you need more than one value, you can also use a generator expression and grab a few at a time. If you need all the values before your program proceeds, use a list comprehension instead.

like image 190
gotgenes Avatar answered Sep 21 '22 06:09

gotgenes


A generator comprehension is the lazy version of a list comprehension.

It is just like a list comprehension except that it returns an iterator instead of the list ie an object with a next() method that will yield the next element.

If you are not familiar with list comprehensions see here and for generators see here.

like image 42
rz. Avatar answered Sep 22 '22 06:09

rz.