I have a list of python objects and I'd like to remove duplicates in the list based on the time value. For example:
class MyClass(object):
identifier = models.CharField(max_length=128)
label = models.CharField(max_length=128)
stat_time = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return str(self.label)
My list may have several instances of MyClass with the same label but different stat_times. I'd like to trim the list and have only one instance of the label with the latest stat_time.
>>> my_list
[MyClass: xxx, MyClass: yyy, MyClass: yyy, MyClass: zzz]
I'd like to end up with:
>>> my_list
[MyClass: xxx, MyClass: yyy, MyClass: zzz]
Here my_list should only contain one instance of MyClass with the 'yyy' label with the latest stat_time.
I hope I have made that clear. Any suggestions much appreciated.