Generator expressions must be parenthesized if not sole argument

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
3 min

banner

# Table of Contents

  1. Generator expressions must be parenthesized if not sole argument
  2. Make sure you don't have a trailing comma after the generator expression
  3. Make sure you haven't misplaced the parentheses when calling a function

# Generator expressions must be parenthesized if not sole argument

The Python error "Generator expressions must be parenthesized if not sole argument" occurs when you pass a non-parenthesized generator expression as an argument to a function that takes multiple arguments.

To solve the error, wrap the generator expression in parentheses and make sure you haven't misplaced your parentheses.

syntaxerror generator expression must be parenthesized if not sole argument

Here is an example of how the error occurs.

main.py
def example(gen, num): print(gen) print(num) # ⛔️ Generator expressions must be parenthesized if not sole argument # ⛔️ SyntaxError: Generator expression must be parenthesized example(num for num in range(3), 10)

The function takes 2 arguments - a generator expression and a number.

We called the function with the two required arguments but we didn't wrap the generator expression in parentheses which caused the error.

Generator expressions must be wrapped in parentheses if they are not the sole function argument.

main.py
def example(gen, num): print(gen) print(num) example((num for num in range(3)), 10)

wrapped the generator expression in parentheses

The code for this article is available on GitHub

We wrapped the generator expression argument in parentheses which resolved the issue.

Wrapping the generator expression in parentheses has a couple of advantages:

  1. It makes your code easier to read.
  2. It makes your code easier for Python to parse.
  3. Python is able to compute the generator expression argument.

If you meant to pass a list to the function, you can wrap the generator expression in square brackets [] to use a list comprehension instead.

main.py
def example(gen, num): print(gen) print(num) example([num for num in range(3)], 10)

using square brackets instead of parentheses

The code for this article is available on GitHub

Wrapping the generator expression in square brackets [] makes it a list comprehension.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

If the function takes a single argument, then you don't have to parenthesize the generator expression.

main.py
def example(gen): print(gen) example(num for num in range(3))

single argument generator expression

# Make sure you don't have a trailing comma after the generator expression

The error is also caused if you mistakenly place a trailing comma after the generator expression, in the function call.

main.py
def example(gen): print(gen) # ⛔️ Generator expressions must be parenthesized if not sole argument # ⛔️ SyntaxError: Generator expression must be parenthesized example(num for num in range(3), ) # 👈️ trailing comma

Notice that there is a trailing comma after the generator expression in the call to the example() function.

  1. If the function takes 2 parameters one of which is a generator expression, then you have to wrap the generator expression in parentheses and supply the second argument.
main.py
# ✅ Works as expected def example(gen, num): print(gen) # <generator object <genexpr> at 0x7f27e6b44580> print(num) # 10 example((num for num in range(3)), 10)
  1. If the function only takes a single parameter which is a generator expression, then remove the trailing comma.
main.py
# ✅ Works as expected def example(gen): print(gen) # <generator object <genexpr> at 0x7f27e6b44580> example(num for num in range(3))
The code for this article is available on GitHub

# Make sure you haven't misplaced the parentheses when calling a function

Another common cause of the error is misplacing the parentheses when calling a function.

Here is an example.

main.py
def example(a, b): return a * b # ⛔️ Generator expressions must be parenthesized if not sole argument # ⛔️ SyntaxError: Generator expression must be parenthesized a_list = [example(num, num + 1 for num in range(3))]

The code sample causes the error because we are trying to call the example() function with a number and a generator expression that is not parenthesized.

Instead, I meant to call the function for each number in range(3).

main.py
# ✅ Works as expected def example(a, b): return a * b a_list = [example(num, num + 1) for num in range(3)] print(a_list) # 👉️ [0, 2, 6]

parentheses placed correctly

The code for this article is available on GitHub

I placed the closing parenthesis ) after the num + 1 expression.

This solves the error because we call the function with two numbers for each iteration of the range() sequence.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.