Skip to content
Home » Generator Expressions in Python

Generator Expressions in Python

Spread the love

Today we’ll be talking about generator expressions.

generator expressions

If you haven’t read my previous six articles on generators (basics of generators, sending objects to generators, the throw method, the yield from statement, recursive generators and passing generators as arguments), it’s definitely recommended to do so before you go on with this one.  

It’s also by all means recommended that you read my article on list comprehensions.

We can code simple generators as expressions. The syntax is very much like in comprehensions, but instead of the square brackets we use parentheses. We choose this option if we want to use the generator right away and its task is not too complicated.

While list comprehensions return lists, generator comprehensions return generators.

Here are some simple examples of generator expressions. First we’ll use a generator expression to generate the squares of all the numbers less than 10:

>>> squares = (i * i for i in range(10))
>>> next(squares)
0
>>> next(squares)
1
>>> next(squares)
4
>>> next(squares)
9
>>> next(squares)
16
>>> next(squares)
25
>>> next(squares)
36
>>> next(squares)
49
>>> next(squares)
64
>>> next(squares)
81
>>> next(squares)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>> 

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

The next example is just a little bit more complicated. Here we want a list of all the letters that make up the last names.

>>> names = ["Bill Hopkins", "Steve Smith", "Mike Clarence"]

If we pass a generator expression to the built-in list function, it’s equivalent to using a list comprehension in the first place:

>>> letters = list(letter for name in names for letter in name.split()[1])
>>> letters
['H', 'o', 'p', 'k', 'i', 'n', 's', 'S', 'm', 'i', 't', 'h', 'C', 'l', 'a', 'r', 'e', 'n', 'c', 'e']

Another example:

words = ["hello", "good", "remember", "unfortunately"]
halves = (word[:int(len(word) / 2)] for word in words)
print(halves)
print(list(halves))

We have the generator comprehension in the second line. What is returned is the halves generator, which can be passed as an argument to the list function. The program prints the first halves of the words in the initial list:

<generator object <genexpr> at 0x00000137FBB49F10>
['he', 'go', 'reme', 'unfort']

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

We can pass a generator expression as an argument to a function. Then we can drop the parentheses enclosing the expression:

>>> sum(n for n in range(5))  # same as sum(0, 1, 2, 3, 4)
10

But we can’t drop the parentheses if the generator expression is not the only item enclosed in the parentheses of the function. Otherwise we’ll get an error:

>>> sorted(n for n in range(5), reverse = True)
  File "<stdin>", line 1
SyntaxError: Generator expression must be parenthesized

For this to work, we’d have to rewrite it as follows:

>>> sorted((n for n in range(5)), reverse = True)
[4, 3, 2, 1, 0]

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Here’s the video version of the article:


Spread the love

Leave a Reply