I have a list of objects, and I want to filter the list in a way that as a result there is only one occurence of each attribute value.
For instance, let's say I have three objects
obj1.my_attr = 'a'
obj2.my_attr = 'b'
obj3.my_attr = 'b'
obj_list = [obj1, obj2, obj3]
And and the end, I want to get [obj1, obj2]
. Actually order does not matter, so [obj1, obj3]
is exactly as good.
First I thought of the typical imperative clunky ways like following:
record = set()
result = []
for obj in obj_list:
if obj.my_attr not in record:
record.add(obj.my_attr)
result.append(obj)
Then I though of maping it to a dictionary, use the key to override any previous entry and finally extract the values:
result = {obj.my_attr: obj for obj in obj_list}.values()
This one looks good, but I would like to know if there any more elegant, efficient or functional way of achieving this. Maybe some sweet thing hidden in the standard library... Thanks in advance.