[Python-Dev] list comprehensions again...

Ka-Ping Yee Ka-Ping Yee <pingster@ilm.com>
Tue, 11 Jul 2000 12:26:54 -0700 (PDT)


On Tue, 11 Jul 2000, Guido van Rossum wrote:
>   [(x,y+z) for x in (1,2,3,4,5,6); for y in "abc" for z in "de"]
> 
> For me, the semicolon separates much stronger than the for, so (to me)
> this feels like (applying parentheses to show grouping):
> 
>   (for x); (for y for z)

When written like the above, i agree that the semicolon separates
more strongly.  Perhaps we should rearrange the syntax to make the
semicolon more interior?

This also addresses Greg's poll:
> 
>         for x in [10, 20, 30]; y in [1, 2]:
>             print x+y
[...]
> *Everyone* voted (B).

How about writing this as:

    for x; y in [10, 20, 30]; [1, 2]:
        print x + y

> And how do you write the other grouping?

As a result, this provides a way to write parallel loops within
list comprehensions, although it might not be very clear until
you're familiar with the way it's written above.  At least it's
possible:
    
    [(x, y+z) for x; y in (1, 2, 3, 4, 5, 6); "abc" for z in "de"]

If colons were introduced, would that help keep things clear?

    [(x, y+z): for x; y in (1, 2, 3, 4, 5, 6); "abc": for z in "de"]

This would then be directly reminiscent of

    for x; y in (1, 2, 3, 4, 5, 6); "abc":
        for z in "de":
            list.append((x, y+z))

which provides the appropriate meaning.

I don't like extra punctuation, but i'm not sure what else to do
here since "for" alone is a very weak separator.


-- ?!ng