Using Python 3.4, I get SyntaxError: invalid syntax
here:
>>> xlist = [1,2,3,4,5]
>>> [yield(x) for x in xlist]
SyntaxError: i
The yield keyword can be used in two ways: As a statement, and as an expression.
The most common use is as a statement within generator functions, usually on its own line and all. It can be used like this:
yield <expr>
yield from <expr>
The yield expression however can be used wherever expressions are allowed. However, they require a special syntax:
(yield <expr>)
(yield from <expr>)
As you can see, the parentheses are part of the syntax to make yield
work as an expression. So it’s syntactically not allowed to use the yield
keyword as an expression without parentheses. That’s why you need to use parentheses in the list comprehension.
That being said, if you want to use list comprehension syntax to create a generator, you should use the generator expression syntax instead:
(x for x in xlist)
Note the parentheses instead of the square brackets to turn this from a list comprehension into a generator expression.
Note that starting with Python 3.7, yield
expressions are deprecated within comprehensions and generator expressions (apart from within the iterable of the left-most for
clause), to ensure that comprehensions are properly evaluated. Starting with Python 3.8, this will then cause a syntax error.
This makes the exact list comprehension in the question a deprecated usage:
>>> [(yield(x)) for x in xlist]
<stdin>:1: DeprecationWarning: 'yield' inside list comprehension
<generator object <listcomp> at 0x000002E06BC1F1B0>