PEP 308: Pep Update

Anders J. Munch andersjm at inbound.dk
Sun Mar 2 20:54:27 CET 2003


"Sheila King" <usenet at thinkspot.net> wrote:
> On Thu, 27 Feb 2003 23:31:03 +0100, "Anders J. Munch"
<andersjm at inbound.dk>
> wrote in comp.lang.python in article
> <Qmw7a.98775$Hl6.9497242 at news010.worldonline.dk>:
>
> > Pop quiz: What is the value of this alternate universe list
> > comprehension expression?
> >
> >    [for x in (1,2): for y in (-1,+1): x*y]
>
> I find this one very confusing.

It's just a nested for-loop.  Remove the braces and add an indent and
you get something that you can actually run at the interpreter prompt,
to print four values:

for x in (1,2):
    for y in (-1,+1): x*y

>Was your intent that it should be clear and
> obvious? Or was your intent to show how this type of thing could be
> confusing? I was not sure.

I showed a list comp. syntax that looks like a for-loop to illustrate
that Python list comprehensions do not look like for loops.  For-loops
have the repeated stuff to the right or below the "for X in V" part,
list comprehensions don't.

- Anders







More information about the Python-list mailing list