list comprehensions *are* nice

Nathan Gray n8gray at caltech.edu.is.my.e-mail.address
Tue May 15 06:50:50 CEST 2001


Andrew Dalke wrote in message <9doc1h$3sp$1 at slb3.atl.mindspring.net>...
>A couple months ago I complained that list comprehensions were
>too confusing and that I would still be using
>
>   results = []
>   for val in data:
>     results.append(..some.expression.using.val..)
>
>I was wrong.

def qsort(lst):
    if len(lst) <= 1: return lst
    return qsort([lessthan for lessthan in lst[1:] if lessthan < lst[0]]) +
\
            [lst[0]] + qsort([grtreq for grtreq in lst[1:] if grtreq >=
lst[0]])

Take that, Haskell!  List comprehensions are rad.

-n8





More information about the Python-list mailing list