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

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


On Tue, 11 Jul 2000, Greg Wilson wrote:
>         for x in [10, 20, 30]; y in [1, 2]:
>             print x+y
[...]
>     (A) 'x' and 'y' move forward at the same rate:
>     (B) 'y' goes through the second list once for each value of 'x':
>     (C) an error message because the two lists are not the same length?
[...]
> *Everyone* voted (B).

Okay, i just thought of another answer to this.

Scream at me if you find this hideous.

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

My previous message proposed colons to separate the clauses of
a list comprehension, which i now realize makes dict comprehensions
impossible (or at least a little strange-looking).  If we use "while",
the lack of semicolons means that we can do parallel loops in
list-comps without colons, which in turn enables dict-comps.

<ducking for cover>


-- ?!ng