Last updated: Apr 11, 2024
Reading time·3 min
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.
Here is an example of how the error occurs.
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.
Generator expressions must be wrapped in parentheses if they are not the sole function argument.
def example(gen, num): print(gen) print(num) example((num for num in range(3)), 10)
We wrapped the generator expression argument in parentheses which resolved the issue.
Wrapping the generator expression in parentheses has a couple of advantages:
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.
def example(gen, num): print(gen) print(num) example([num for num in range(3)], 10)
Wrapping the generator expression in square brackets []
makes it a list
comprehension.
If the function takes a single argument, then you don't have to parenthesize the generator expression.
def example(gen): print(gen) example(num for num in range(3))
The error is also caused if you mistakenly place a trailing comma after the generator expression, in the function call.
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.
# ✅ 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)
# ✅ Works as expected def example(gen): print(gen) # <generator object <genexpr> at 0x7f27e6b44580> example(num for num in range(3))
Another common cause of the error is misplacing the parentheses when calling a function.
Here is an example.
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)
.
# ✅ 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]
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.
You can learn more about the related topics by checking out the following tutorials: