Get a key with maximum value in a dictionary in Python
This post will discuss how to return a key with maximum value in a dictionary in Python.
We can use the in-built function max() to return a key with maximum value in a dictionary in Python. The max() function takes an optional argument called key, that is an ordering function to be used for comparison, and returns the largest item in an iterable. There are several ways to use this function:
1. Using operator.itemgetter() function
We can use the dict.item() function to get the list of items in the dictionary and operator.itemgetter() as an ordering function. The operator.itemgetter() function returns a callable object that fetches an item from its operand using its index. The idea is to use this function to create a custom comparison function that returns the value for a given key from a dictionary. We can then pass this function as an argument for key to max(), which will return the maximum key based on its value. Here is an example of how to use this function:
|
1 2 3 4 5 6 7 8 |
import operator d = {'A': 1, 'B': 2, 'C': 3} max_pair = max(d.items(), key=operator.itemgetter(1)) print(max_pair) # print the pair ('C', 3) print(max_pair[0]) # print the key 'C' |
This has the advantage over other approaches in that it returns the item with the maximum value, while other functions only return the key with the maximum value.
2. Using dict.get() function
Another option is to use the dict.get() function as the key to the max() function, which returns the key with maximum value when the dictionary iterator is used as iterable. Here is an example of how to use it:
|
1 2 3 4 |
d = {'A': 1, 'B': 2, 'C': 3} max_key = max(d, key=d.get) print(max_key) # C |
Here’s an alternate version which uses a lambda function to create a custom comparison function that returns the value for a given key from a dictionary. We can then pass this function as an argument for key to max(), which will return the maximum key based on its value.
|
1 2 3 4 |
d = {'A': 1, 'B': 2, 'C': 3} max_key = max(d, key=(lambda k: d[k])) print(max_key) # C |
3. Using Inverse Dictionary
A third option is to invert each key/value pair of the dictionary. For example, dictionary { k1: v1, k2: v2, … , kn: vn} should be transformed into a dictionary { v1: k1, v2: k2, … , vn: kn}. This can be easily done using dictionary comprehension, as shown below:
|
1 2 3 4 5 6 |
d = {'A': 1, 'B': 2, 'C': 3} inv = {v: k for k, v in d.items()} max_key = inv[max(inv)] print(max_key) # C |
Alternatively, we can write:
|
1 2 3 4 5 6 |
d = {'A': 1, 'B': 2, 'C': 3} m = max(d.values()) max_key = next(k for k, v in d.items() if v == m) print(max_key) # C |
4. Using collections.Counter() class
We can create a Counter object from a dictionary and then use its most_common() function, which returns a list of the most common elements and their counts from the Counter object. We can then access the first element of this list, which will be a tuple containing the key with the maximum value and its count. Here is an example of how to use this function:
|
1 2 3 4 5 6 7 8 |
import collections d = {'A': 1, 'B': 2, 'C': 3} counter = collections.Counter(d) print(counter) # Counter({'C': 3, 'B': 2, 'A': 1}) print(counter.most_common(1)) # [('C', 3)] print(counter.most_common(1)[0][0]) # C |
That’s all about getting a key with maximum value in a dictionary in Python.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)