List comprehension

Terry Reedy tjreedy at
Fri Dec 30 23:31:20 EST 2016

On 12/30/2016 2:37 PM, Jason Friedman wrote:

> Now, this puzzles me:
>>>> [x,y for a in data]
>   File "<stdin>", line 1
>     [x,y for a in data]
>            ^
> SyntaxError: invalid syntax

I believe that python begins to parse this as
[x, (y for a in data)], a list of 2 items,
except that the required () are missing.
Notice that the ^ is under the r of 'for'.
"y for" is not a legal beginning of a list item.
Most tuples need ()s for proper grouping.

Terry Jan Reedy

More information about the Python-list mailing list