Generator expressions are very similar to list comprehensions. The main difference is that it does not create a full set of results at once; it creates a generator object which can then be iterated over.
For
instance, see the difference in the following code:
List comprehension
[x**2 for x in range(10)]
# Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Generator comprehension
(x**2 for x in range(10))
# Output: <generator object <genexpr> at
0x11b4b7c80>
These
are two very different objects:
the list
comprehension returns a list object whereas the generator comprehension returns a generator.
generator objects cannot be indexed and makes
use of the next function to get items in order.
Use cases
Generator expressions are lazily evaluated, which means that they generate and return each value only when the generator is iterated. This is often useful when iterating through large datasets, avoiding the need to create a duplicate of the dataset in memory:
for square in (x**2 for
x in range(1000000)):
def mygenerator():
print('First item')
yield 10
print('Second item')
yield 20
print('Last item')
yield 30
>>> gen = mygenerator() >>> next(gen) First item 10 >>> next(gen) Second item 20 >>> next(gen) Last item 30
Comments
Post a Comment