# [Python-ideas] New 3.x restriction in list comprehensions

Terry Reedy tjreedy at udel.edu
Fri Sep 17 23:32:04 CEST 2010

```On 9/17/2010 3:44 PM, Raymond Hettinger wrote:
> In Python2, you can transform:
>
>    r = []
>    for x in 2, 4, 6:
>         r.append(x*x+1)

for x in 2,4,6:
yield x*x+1

also works in 2/3.x
>
> into:
>
>     r = [x*x+1 for x in 2, 4, 6]
>
> In Python3, the first still works but the second gives a SyntaxError.
> It wants the 2, 4, 6 to have parentheses.
>
> The good parts of the change:
>   + it matches what genexps do

Is the restriction necessary for genexps? If the parser could handle
[x*x+1 for x in 2, 4, 6]
is
(x*x+1 for x in 2, 4, 6)
impossible, perhaps due to paren confusion?

>   + that simplifies the grammar a bit (listcomps bodies and genexp bodies)
>   + a listcomp can be reliably transformed to a genexp
>
>   + The restriction wasn't necessary (we could undo it)
>   + It makes 2-to-3 conversion a bit harder
>   + It no longer parallels other paren-free tuple constructions:
>          return x, y
>          yield x, y
>          t = x, y
>             ...
>   + It particular, it no longer parallels regular for-loop syntax
>
> The last part is the one that seems the most problematic.
> If you write for-loops day in and day out with the unrestricted
> syntax, you (or least me) will tend to do the wrong thing when
> writing a list comprehension.  It is a bit jarring to get the SyntaxError
> when the code looks correct -- it took me a bit of fiddling to figure-out
> what was going on.
>
> My question for the group is whether it would be a good
> idea to drop the new restriction.

3.x is in a sense more consistent than 2.x in that converting a for loop
with a bare tuple always requires addition of parentheses rather than
just sometimes. Never requiring parens would be even better to me if it
did not make the implementation too messy.

--
Terry Jan Reedy

```