List comprehensions' ugliness (Was: Re: How to explain exactly what "def" does?)

Skip Montanaro skip at pobox.com
Wed Feb 5 15:52:07 EST 2003


Okay, so you have a function, f, which gives you the true/false descision
filter needs:

    yeah_baby_yeah = filter(f, baby_list)

or

    yeah_baby_yeah = [baby for baby in baby_list if f(baby)]

How do you "filter-out"?  With filter() I think you need to write another
(admittedly trivial) function, g:

    def g(item): return not f(item)

or for the functionally inclined:

    g = lambda(item): not f(item)

With list comprehensions, you just add "not":

    no_baby_no = [baby for baby in baby_list if not f(baby)]

Maybe you need to parse that just like the filter-equivalent version, but I
think it shows some things are easier to do one way, others the other way.  

We're all picking nits here folks.  Filter() not likely to go away anytime
soon, and I doubt list comprehensions are either.  I don't think there is
any more light to be shed on the topic.  It's all just heat at this point.

can-we-please-just-have-a-good-old-fashioned-{-}-war?-ly, y'rs,

Skip





More information about the Python-list mailing list