[Python-Dev] List comprehension syntax (Features for Python 2.0)

Skip Montanaro skip@mojam.com (Skip Montanaro)
Tue, 25 Jul 2000 13:41:17 -0500 (CDT)


    >> [for x in range(100): x*2]

    Fredrik> fwiw, I read that as:

    Fredrik>     [
    Fredrik>         for x in range(100):
    Fredrik>             x * 2
    Fredrik>     ]

    Fredrik> which makes no sense at all.

It makes perfect sense.  You read it correctly as far as I can tell.  Using
Greg's patch, I get what you'd expect from a simple for loop:

    >>> [x*2 for x in range(10)]
    [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

It occurred to me a little bit ago to suggest that we just allow complete
statements within list constructors.  The semantics would be that any
objects left on the stack at the end of the statements would constitute the
elements of the list.  Is that too weird?

Skip