Example 1: python find the key with max value
a_dictionary = {"a": 1, "b": 2, "c": 3}
max_key = max(a_dictionary, key=a_dictionary.get)
print(max_key)
Example 2: python get the key with the max or min value in a dictionary
key_with_max_value = max(dictionary, key=dictionary.get)
max_value = dictionary[max(dictionary, key=dictionary.get)]
max_value = max(dictionary.values())
dictionary = {"a": 1, "b": 2, "c": 3}
max(dictionary, key=dictionary.get)
--> 'c'
Example 3: find the max value in dictionary python
my_dict = {'a': 5, 'b': 10, 'c': 6, 'd': 12, 'e': 7}
max(my_dict, key=my_dict.get)
Example 4: 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 5: max element in dictionary python
import operator
stats = {'a':1000, 'b':3000, 'c': 100}
max(stats.iteritems(), key=operator.itemgetter(1))[0]
Example 6: get max n values in dict py
import heapq
from operator import itemgetter
n = 3
items = {'a': 7, 'b': 12, 'c': 9, 'd': 0, 'e': 24, 'f': 10, 'g': 24}
topitems = heapq.nlargest(n, items.items(), key=itemgetter(1))
topitemsasdict = dict(topitems)