Using generator expressions

We've shown some examples of generator expressions already. We'll show some more later in the chapter. We'll introduce some more generator techniques in this section.

It's common to see generator expressions used to create the list or dict literals through list comprehension or a dict comprehension syntax. This example, [x**2 for x in range(10)], is a list comprehension, sometimes called a list display.  For our purposes, the list display (or comprehension) is one of several ways to use generator expressions.  A collection display includes the enclosing literal syntax. In this example, the list literal [] characters wrap the generator: [x**2 for x in range(10)]. This is a list comprehension; it creates a list

Get Functional Python Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.