June 2017
Beginner
352 pages
8h 39m
English
Generator expressions are a cross between comprehensions and generator functions. They use a similar syntax as comprehensions, but they result in the creation of a generator object which produces the specified sequence lazily. The syntax for generator expressions is very similar to list comprehensions:
( expr(item) for item in iterable )
It is delimited by parentheses instead of the brackets used for list comprehensions.
Generator expressions are useful for situations where you want the lazy evaluation of generators with the declarative concision of comprehensions. For example, this generator expression yields a list of the first one-million square numbers:
>>> million_squares = (x*x for x in range(1, 1000001))
At this ...
Read now
Unlock full access