how to get the key with the highest value from dictionary in python code example
Example 1: python how to find the highest number in a dictionary
a_dictionary = {"a": 1, "b": 2, "c": 3}
max_key = max(a_dictionary, key=a_dictionary.get)
get key with max value
print(max_key)
Example 2: python max key dictionary key getter
from operator import itemgetter
items = [
{'a': 1, 'b': 99},
{'a': 2, 'b': 88},
{'a': 3, 'b': 77},
]
print(max(items, key=itemgetter('a')))
print(max(items, key=itemgetter('b')))
Example 3: find the highest 3 values in a dictionary.
>>> sorted(my_dict, key=my_dict.get, reverse=True)[:3]
['K', 'B', 'A']