[Python-Dev] list comprehensions again...

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


On Tue, 11 Jul 2000, Greg Wilson wrote:
> (either now or in future).  Would it also permit:
> 
>       [{x:y} for x in (1,2,3) for y in (4,5,6)]
> 
> i.e. dict construction using list comprehension?  I'd use this in quite a
> few places.

The above would produce

    [{1: 4}, {1: 5}, {1: 6}, {2: 4}, {2: 5}, {2: 6}, {3: 4}, {3: 5}, {3: 6}]

If, on the other hand, you tried to make a single dictionary with

    {x: y for x in (1, 2, 3) for y in (4, 5, 6)}

it doesn't make a lot of sense (multiple y's for the same x?).  The
only thing i could imagine it producing, perhaps, is

    {1: 6, 2: 6, 3: 6}


-- ?!ng