List comprehensions

Alexander Williams thantos at chancel.org
Wed Dec 22 05:51:39 EST 1999


On 22 Dec 1999 10:28:52 GMT, Albert Hofkamp <hat at se-46.wpa.wtb.tue.nl>
wrote:

>If it is that easy to construct a list of tuples, then I'd think to drop
>parallel iteration if only to prevent cluttering of the semantics of the
>list comprehension.

The ease of constructing lists of tuples (and the ambiguity of
parallel constructions /within/ List Comprehensions) help explain why
Haskell, certainly one of the reigning princess of List Comprehension,
has the zip() function which operates exactly like map(None) in
Python.

>>>   [ x>6, x>5 ]
>>>
>>Easy, a two element list consisting of two Boolean values dependent on
>>whether or not the pre-defined value x makes said statement true.  :)
>
>ROFL !
>
>Does this lead to the conclusion that there are some syntactical
>problems with list comprehension ?

Not really.  Its exactly the same answer you get in Python /without/
LCs.  Keep in mind, the > and < operators are Boolean and return 0 or
1 based on whether the truth value is true or false (in Haskell, one
has pre-defined Boolean True and False instead.  What you have above,
properly, isn't an LC at all but simple list /construction/, creating
a two element list from the results of executing the two tests.

Interestingly, it does /exactly/ the same thing in Haskell.

More properly, you don't have an LC /until/ (given the syntax we've
been using) you have the [ | ] construct, the 'extraction bar' being
absolutely necessary to distinguish simple construction from LC.

> i := 30
> ys := [ 4 ]
> xs := [ i ] + [ i+15, for i in ys ] + [ i+1 ]
>
>is well-defined.
>I'd like to have xs == [ 30, 19, 31 ] at the end, but I am too new to
>Python to say anything useful about scoping.
>Is this what you'd expect, and is it feasible ?

Certainly; I don't expect scoping in the LCs will be much of an
issue.  The above mock-up should be completely defined under present
scoping rules.

-- 
Alexander Williams (thantos at gw.total-web.net)           | In the End,
  "Join the secret struggle for the soul of the world." | Oblivion
  Nobilis, a new Kind of RPG                            | Always
  http://www.chancel.org                                | Wins



More information about the Python-list mailing list