[Python-Dev] list comprehensions again...
Thomas Wouters
thomas@xs4all.net
Tue, 11 Jul 2000 17:09:05 +0200
[Note: removing known python-dev members from CC: line. I apologize if
that's inconvenient, but otherwise Mailman might not let the posting
through. Also, Greg Ewing already received this msg, which had a typo in the
To: line. Sorry, Greg ;-P]
On Tue, Jul 11, 2000 at 10:42:55AM -0400, Greg Wilson wrote:
> > > [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?
Oh, yeah, I forgot to say why I disliked it, asside from it being hard to
do: Isn't the idea that ()'s do not create tuples, ','s do ?
To make something clear, though, '[x,y for x in (1,2,3) for y in (4,5,6)]'
creates
[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
and *not* '[1, 4, 2, 5, 3, 6]', in case anyone was thinking that ! That
would be the only reason I'd go for forcing '(x,y)' somehow: disambiguation.
But then, tuple-creation is slightly ambiguous already, so allowing the ()'s
to be left out here as well, seems pretty natural to me.
> 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.
The first is a good example, though it creates a list of lists, not a single
list. The second would create a list of dicts, not a single dict. If you
want to create a single dict, this would be better syntax:
{x:y for x in (1,2,3) for y in (4,5,6)}
AKA dict-comprehensions ;) Unless '[' .. ']' is changed from list-creator to
comprehensions-function, and the resulting object is determined by the first
expression within the listcomp... That makes it a lot less logical, to me :P
> 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.
Well, what would be the use ? You can do that using nested for-loops
already. So it looks a tad different, so what ;) (Or are you talking about
for-for-loops in list comprehensions instead ?)
> 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".
Well, the problem with *that* syntax is that it's already valid ! Imagine
seq1 being a sequence, and seq2.__contains__ returning a sequence. Also, the
parser would have a hard time figuring out what version of the for loop to
use: is this
'for' exprlist 'in' testlist
where 'testlist' contains 'and' and/or 'in', or
'for' exprlist 'in' testlist ('and' exprlist 'in' testlist)*
? I'm not sure if it's possible to use the 2nd form at all!
--
Thomas Wouters <thomas@xs4all.net>
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!