Below we sort a dictionary, then find the key with the highest value.
d = {'a': 10, 'b': 20, 'c': 30}
sorted_items = sorted(d.items(), key=lambda item: item[1])
print(sorted_items[-1][0])- Get
items()from the dictionary. - Pass the to
sortedand pass a lambda to in key parameter. This tells the sorted() function whhat to sort on. - Get the last entry in the sorted list of entries.
- In this case, since we want the key for the kighest value, get the first item from the tuple in the first entiy in the sorted items.