I want a function that removes values from a list if a predicate evaluates to True. The best I could come up with is:
def extract(lst, pred): idx = 0 ret = [] for obj in lst[:]: if pred(obj): ret.append(obj) lst.pop(idx) else: idx += 1 return ret Anybody have a better, more Pythonic solution? One of my failed attempts was this code, which fails when the predicate itself has "state": def extract(lst, pred): # BAD: Would not work in a case like pred = "extract every other object" ret = filter(lst, pred) for obj in ret: lst.remove(obj) return ret
-- http://mail.python.org/mailman/listinfo/python-list