List comprehensions

Alexander Williams thantos at chancel.org
Wed Dec 22 17:54:30 EST 1999


On 22 Dec 1999 12:43:33 GMT, Albert Hofkamp <hat at se-46.wpa.wtb.tue.nl>
wrote:

>This is even shorter than the map() call.
>Sorry for asking, but is it safe to assume that
>
>  for x,y in zip(xs,ys):
>    print x,y
>
>works as expected  (and in list comprehensions as well then) ?
>(I am still a terribly newbie at Python.
> It is a fascinating language, you learn new constructs every day !!
>)

Well, it /would/ if Python had zip().  :)  Instead, it has 
map(None, lst1, lst2) which does the same thing.  

>My basic problem with fold was that is is too much hassle, but that is
>not the fault of fold, but of our language, because it doesn't allow
>writing expressions as a name-less function.

Right; once you have lambdas, there is a significant bonus in things
like map(), filter(), reduce(),and fold().  Of course, its nearly
trivial to create named functions in Python and often The Right Thing
to make the temporary tools you use in such constructs names,
especially if you find yourself using them more than once or twice.

>Sure, just implement lazy evaluation :-)

Yeah, like that'll happen in Python.  Much as I love Python, I don't
expect such things to become part of the language, ever.  Even though
parsing streams as lazy lists is incredibly cool.  :)

>Tricks like you explain with zip() are not part of their definition.
>I have read the Python tutorial, and saw these two functions, and
>assumed that I understood what they meant. Apparently, I missed an
>extension (wrt Bird&Wadler) that allows zip()-like functionality.

Actually, you only missed a bit of the definition, to wit, what the
map/filter does when None is given as the function to use on the
lists.  Map creates tuples of the elements in parallel, filter returns
a list of those elements which evaluate to True.  Special cases, but
handy because they exist.  No extensions at all.

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