[Python-Dev] re: list comprehension / pop quiz

Ka-Ping Yee Ka-Ping Yee <pingster@ilm.com>
Tue, 11 Jul 2000 15:02:17 -0700 (PDT)


On Tue, 11 Jul 2000, Jeremy Hylton wrote:
> If we end up calling this new feature list comprehensions, we might
> create the expectation that they behave like list comprehensions in
> other languages.

Good point.

> >[ f x | x <- xs ]
[...]
> >[ (x,y) | x <- xs, y <- ys ]

The closest translations for these i can imagine are

    [ f(x), for x in xs ]

    [ (x,y), for x in xs, for y in ys ]

I'd say the above has to be about my favourite for ease of reading.

I think the following grammar would work:

    listmaker: test [',' (list_iter | [test (',' test)* [',']])]
    list_iter: list_for | list_if
    list_for: 'for' exprlist 'in' testlist [',' list_iter]
    list_if: 'if' test [',' list_iter]


-- ?!ng