List comprehension...

William Tanksley wtanksle at hawking.armored.net
Sun Dec 12 20:25:28 EST 1999


On 13 Dec 1999 01:39:12 +0100, Magnus L. Hetland wrote:

>Is list comprehension on its way into the language? Or is that just a
>rumour, based on Greg's patch?

I hope it's going it.

>And - as allways, I can't leave the syntax alone without a suggestion
>of my own... Greg's patch works like this (among other things):

The amusing thing is that I tried to do the exact same thing as you're
about to try.

>  [(i, s) for i in nums for s in strs]
>To me, this sounds a bit like
>  [(i, s) for i in [nums for s in strs]]

>which I guess doesn't give much meaning. I think the syntax would be
>clearer (and more natural language-like) with an "and" instead of the
>"for"s, after the first one. The first ones marks that this is the
>list of (i, s) for all of the "i"s and all of the "s"s given. Thus:

>  [(i, s) for i in nums and s in strs]

First of all, notice that "nums and s" is a valid parse of the middle of
this string.  Thus, it's ambiguous.

Second, even if it wasn't, you forgot that 'if' is a valid keyword in such
a comprehension.

>  Magnus

-- 
-William "Billy" Tanksley, in hoc signo hack



More information about the Python-list mailing list