Personally, I like the version you cited, assuming you already have a list of goodvals
hanging around. If not, something like:
good = filter(lambda x: is_good(x), mylist)bad = filter(lambda x: not is_good(x), mylist)
Of course, that's really very similar to using a list comprehension like you originally did, but with a function instead of a lookup:
good = [x for x in mylist if is_good(x)]bad = [x for x in mylist if not is_good(x)]
In general, I find the aesthetics of list comprehensions to be very pleasing. Of course, if you don't actually need to preserve ordering and don't need duplicates, using the intersection
and difference
methods on sets would work well too.