Python, remove duplicates from list of tuples

alexvassel picture alexvassel · Jun 29, 2011 · Viewed 8.8k times · Source

I have the following list:

[('mail', 167, datetime.datetime(2010, 9, 29)) , 
 ('name', 1317, datetime.datetime(2011, 12, 12)), 
 ('mail', 1045, datetime.datetime(2010, 8, 13)), 
 ('name', 3, datetime.datetime(2011, 11, 3))]

And I want to remove items from the list with coinciding first item in a tuple where date is not the latest. In other words I need to get this:

[('mail', 167, datetime.datetime(2010, 9, 29)) , 
 ('name', 1317, datetime.datetime(2011, 12, 12))]

Answer

Björn Pollex picture Björn Pollex · Jun 29, 2011

You can use a dictionary to store the highest value found for a given key so far:

temp = {}
for key, number, date in input_list:
    if key not in temp: # we see this key for the first time
        temp[key] = (key, number, date)
    else:
        if temp[key][2] < date: # the new date is larger than the old one
            temp[key] = (key, number, date)
result = temp.values()