I have list consisting with replacements and I want to do two things:
- remove duplicates
- remove all elements by a specific criteria, to be exact I want to remove all elements bigger than a certain value.
I figured I can use filter for 2 and than use set to achieve 1 something like
list(set(filter(lambda x:x<C, l)))
is there a better/more pythonic/more efficient way?
In my opinion the most Pythonic way to filter sets, wheevern possible, is by use set operations (Venn diagrams) :
another example when you just want to remove value 5 from set A you just type:
and as in this question if you need to filter for grater values than C you just type "containment check" operator "in" which in Python code executes sets.contains() magic method (magic method shouldn't be called directly that's why you use "in"):
The best two ways to do them are filter:
Or set comprehensions (which many would consider more pythonic, and even more efficient):
But I guess, if you’re familiar with filter, that you can just stick to it.
Using list comprehension is maybe more "pythonic".