filter based on set membership

I would love

small_evens = set([2, 4, 6, 8])
only_small_odds = filter(not in small_evens, [1, 3, 4, 1, 1, 2, 7, 6, 4, 5])

but of course this is a syntax error.

both filter(lambda x: x not in set, items) and [x for x in items if x not in set] feel too verbose.

Is there another way?

I suspect there might be because, for example, newer Python versions have made map(str.lower, strings) possible (used to have to map(lambda s: s.lower(), strings))



Read more here: https://stackoverflow.com/questions/64403803/filter-based-on-set-membership

Content Attribution

This content was originally published by theonlygusti at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: