Partition list with predicate

Steve Holden steve at holdenweb.com
Wed Apr 23 15:26:37 EDT 2008


Terry Reedy wrote:
> "Jared Grubb" <jared.grubb at gmail.com> wrote in message 
> news:925822270804230959v557ec5f5re02737709f94d3c6 at mail.gmail.com...
> | I want a function that removes values from a list if a predicate 
> evaluates
> | to True.
> 
> Forget the rigamarole you posted, which has several defects.
> If you must modify the list in place, because you have multiple references 
> to it:
> 
> lst[:] = filter(lambda x: not pred(x), lst)
> 
Wouldn't

lst[:] = [x for x in lst if not pred(x)]

be more direct?

> Otherwise, just lst = filter(....)
> 
And similarly

lst = [x for x in lst if not pred(x)]

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list