Python: partition an iterable

Sep 28, 2015 20:43

"Partition" an iterable into two sets according to some predicate. (Ab)using the fact that set.add returns None.

bad = set()
good = {x for x in it if is_good(x) or bad.add(x)}

python

Previous post Next post
Up