knifenomad wrote: > i know it's not very hard to get that solution. > just by implementing simple function like below. > > def partition(target, predicate): > """ > split a list into two partitions with a predicate > provided. > any better ideas? :) > """ > true = [] > false= [] > for item in target: > if predicates(item): > true.append(item) > else: > false.append(item) > return true, false > > but i wonder if there's another way to do this with standard libraries > or .. built-ins. > if it's not, i'd like the list objects to have partition method
I've oft collected partitions in a dict, so I'll suggest the generalization: def partition(target, predicate): result = {} for item in target: result.setdefault(predicate(item), []).append(item) return result > true, false = [1,2,3,4].partition(lambda x: x >1) > > print true, false > [2,3,4] [1] With my version you'd get: {False: [1], True: [2, 3, 4]} -- --Bryan -- http://mail.python.org/mailman/listinfo/python-list