[Python-Dev] list comprehensions again...

Greg Wilson gvwilson@nevex.com
Tue, 11 Jul 2000 10:42:55 -0400 (EDT)


> >    [x,y for x in (1,2,3) for y in (4,5,6)]
> Hm, I suppose the programmer meant the same as
>     [(x,y) for x in (1,2,3) for y in (4,5,6)]
> Would it be really bad if we forced them to write it that way?

Strongly prefer the latter, as it permits:

      [[x,y] for x in (1,2,3) for y in (4,5,6)]

(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.

> Side note: the suggested syntax here (for i in seq1 for j in seq2)
> looks like a potential alternative for the currently proposed parallel
> for loop syntax (for i in seq1; j in seq2).
> 
> Only problem: the for-for variant could be mistaken by someone used to
> see nested loops as meaning the same as
> 
>     for i in seq1:
> 	for j in seq2: ...

Until yesterday's posting, I assumed that this is what for-for loops would
do, i.e. that they'd deliver a cross-product rather than one-from-each.
Was going to ask whether:

    for i in seq1 and j in seq2:
        foo

would be clearer, but then realized that people might assume it implied
that:

    for i in seq1 or j in seq2:
        foo

would also work, meaning either "while either list is non-empty" or
"cross-product".

Random thoughts,
Greg