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)])
Padraic cunningham
source share