In my opinion, the most Pythonic way to filter sets where possible is to use set operations (Venn diagrams):
A = {0, 1, 4, 5, 8}; B = {2, 1, 3, 4, 6}; print("Union :", A | B) print("Intersection :", A & B) print("Difference :", A - B) print("Symmetric difference :", A ^ B)
Another example, when you just want to remove the value 5 from set A, simply type:
A - {5,}
and as in this question, if you need to filter for larger values than C, you simply type the “content validation statement” into “in”, which sets up in Python code. the magic method contains () (the magic method should not be called directly, so you use the "in"):
{x for x in l if x > C}
source share