List Of Objects With A Unique Attribute
I have a list of objects that each have a specific attribute. That attribute is not unique, and I would like to end up with a list of the objects that is a subset of the entire li
Solution 1:
You can use a list comprehension
and set
:
objects = (object1,object2,object3,object4)
seen = set()
unique = [obj for obj in objects if obj.thing not in seen and not seen.add(obj.thing)]
The above code is equivalent to:
seen = set()
unique = []
for obj in objects:
if obj.thing notin seen:
unique.append(obj)
seen.add(obj.thing)
Post a Comment for "List Of Objects With A Unique Attribute"