[Tutor] build list of non-empty variables

John Fouhy john at fouhy.net
Thu Jul 10 03:38:33 CEST 2008


On 10/07/2008, Kent Johnson <kent37 at tds.net> wrote:
>  The actual formal syntax definitions for the two are slightly different:
>  http://docs.python.org/ref/lists.html
>  http://docs.python.org/ref/genexpr.html
>
>  Presumably this means there is something that is syntactically allowed
>  in one form and not the other, but I can't figure out what it might
>  be.

Is the generator expression grammar right?  How do I parse, e.g.,
'(x+1 for x in range(10))'?  Seems like there's nothing there for
'range(10)'.  Like it should replace 'or_test' with 'old_expression'.

At any rate, here is one difference:

>>> a = range(5)
>>> b = range(5, 10)
>>> [x for x in a, b]
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
>>> (x for x in a, b)
  File "<stdin>", line 1
    (x for x in a, b)
                 ^
SyntaxError: invalid syntax

(I'm not sure I've ever used a list comprehension like that)

-- 
John.


More information about the Tutor mailing list