Python generator expression parentheses oddity Python generator expression parentheses oddity python python

Python generator expression parentheses oddity


It is legal, and the general rule is that you do need parentheses around a generator expression. As a special exception, the parentheses from a function call also count (for functions with only a single parameter). (Documentation)

Note that testing if my_string appears in my_list is as easy as

my_string in my_list

No generator expression or call to any() needed!


It's "legal", and expressly supported. The general rule is "((x)) is always the same as (x)" (even though (x) is not always the same as x of course,) and it's applied to generator expressions simply for convenience.