First, find duplicates in the list

I have a list that looks like this:

[(1, 0.3), (3, 0.2), (3, 0.15), (1, 0.07), (1, 0.02), (2, 0.01)] 

I want to keep the first duplicates found in this list, based on the first element in each tuple:

 [(1, 0.3), (3, 0.2), (2, 0.01)] 

Is there an effective way to do this?

+7
python list
source share
2 answers

If you use the generator function, you can lazily return the first cheats, all you need to save are the keys that will be executed after the function finishes:

 def first_found(l): seen = set() for k, v in l: if k not in seen: yield (k, v) seen.add(k) 

What for your list will give you:

 print(list(first_found(l))) [(1, 0.3), (3, 0.2), (2, 0.01)] 

Or update the source list:

 l[:] = first_found(l) 

Or create a dict file:

 od = OrderedDict(first_found(l)) print(od) OrderedDict([(1, 0.3), (3, 0.2), (2, 0.01)]) 
+4
source share

If the order of the resulting list does not matter, only it contains the first record from the source list for each tuple: first cancel the list, then pass it through dict to remove duplicates and save the last record for each (the first in the original list, since it was canceled) :

 >>> items = [(1, 0.3), (3, 0.2), (3, 0.15), (1, 0.07), (1, 0.02), (2, 0.01)] >>> list(dict(reversed(items)).items()) [(1, 0.3), (2, 0.01), (3, 0.2)] 

If the order of the resulting list matters, see Padraics answer :)

+7
source share

All Articles