I’m working with a list of dict objects that looks like this (the order of the objects differs):
[
{'name': 'Foo', 'score': 1},
{'name': 'Bar', 'score': 2},
{'name': 'Foo', 'score': 3},
{'name': 'Bar', 'score': 3},
{'name': 'Foo', 'score': 2},
{'name': 'Baz', 'score': 2},
{'name': 'Baz', 'score': 1},
{'name': 'Bar', 'score': 1}
]
What I want to do is remove duplicate names, keeping only the one of each name that has the highest 'score'. The results from the above list would be:
[
{'name': 'Baz', 'score': 2},
{'name': 'Foo', 'score': 3},
{'name': 'Bar', 'score': 3}
]
I’m not sure which pattern to use here (aside from a seemingly idiotic loop that keeps checking if the current dict‘s 'name' is in the list already and then checking if its 'score' is higher than the existing one’s 'score'.
One way to do that is:
so output will be: