Python dictionaries allow you to store value in key-value format.
You can get the key with a maximum value in a dictionary using the max(yourdict, key = yourdict.get) statement.
This tutorial teaches you the methods available to get the key with a maximum value in a dictionary.
If You’re in Hurry…
You can use the max()
function to get the key with a maximum value.
Max function returns the largest item in an iterable.
yourdict = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"fourr": 4
}
key_with_max_value = max(yourdict, key = yourdict.get)
key_with_max_value
Output
four
If You Want to Understand Details, Read on…
Different methods are available to get the key with a maximum value in a dictionary. Let us learn each method in detail.
Table of Contents
Using Max Function and Dict.Get
The max() function returns the largest item in the iterable.
It accepts one mandatory parameter and two optional parameters.
iterable
– Mandatory – an iterable from which the maximum value needs to be identifiedkey
– Optional – Argument specifying an ordering function. Here, the value of each key is ordered, and the maximum value’s key is returned when you passdict.get
default
– Optional – a default value to return when the iterable is empty. If this is not passed,ValueError
will be raised when the dictionary is empty
If multiple items are maximal, the first identified item’s key will be returned.
Code
The code below demonstrates how to find the key with a maximum value in a dictionary using the max()
function.
yourdict = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"fourr": 4
}
key_with_max_value = max(yourdict, key = yourdict.get)
key_with_max_value
Output
The key of the maximum value in the dictionary is returned.
four
Using Operator.itemgetter
This section teaches you how to use the operator module’s itemgetter
to get the key with a maximum value of a dictionary.
The itermgetter()
method returns a callable object that fetches an item from its operand.
Finding the key of the maximum value using the itemgetter()
works as explained below:
In the max function,
yourdict.items()
– Returns both key and value to iteratorkey = operator.itemgetter(1)
– Specifies that the sorting function to find the maximum value should use the value in the index 1. For example – a tuple withkey
andvalue
will be available during iteration. Key is available in index0
, and value is available in index1
. When you pass1
to theitemgetter()
, the item in index1
will be returned to the calling function. This means the value will be used to find the largest item- After finding the largest value, the
max()
function returns the tuple. For example, (‘four’, 4). You can use the index -> [0] to find the key.
Code
The code below demonstrates how to use the itemgetter()
method to find the key of the maximum value in a dictionary.
import operator
yourdict = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"fourr": 4
}
key_with_max_value = max(yourdict.items(), key = operator.itemgetter(1))[0]
key_with_max_value
Output
'four'
Get All Keys With Max Value
Dictionaries can contain the same value for more than one key.
In this case, you must get all the keys with the maximum value to see if there are any duplicates of the maximum value. There are no direct methods available for this.
To get all the keys of the maximum value,
- Find the maximum value in the dictionary using
max(yourdict.values())
- Using list comprehension, iterate over the dictionary items and check if the value is equal to the identified maximum value.
- If it is equal, then return the key to the resultant list of the list comprehension
This is how you can get all the keys of the maximum value in the dictionary.
Code
The code below demonstrates how to get all the keys of the maximum value in the dictionary.
yourdict = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"fourr": 4
}
max_value = max(yourdict.values())
keys_with_max_values = [k for k,v in yourdict.items() if v == max_value]
keys_with_max_values
Output
['four', 'fourr']
Getting Maximum Value in Dictionary
To get the maximum value in the dictionary, you can use the dict.values()
method to the max()
function.
The dict.values()
method returns the values of all keys as a list, and then the max function identifies the largest item in the list and returns it.
yourdict = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"fourr": 4
}
maximum_val = max(yourdict.values())
print("Maximum Value in the dictionary : ", maximum_val)
Output
Maximum Value in the dictionary : 4
Conclusion
You’ve learned how to get the key with a maximum value in a dictionary in Python.
There may be cases where more than one key contains the largest value. In this case, you’ve also learned how to get all the keys with a maximum value in a dictionary using list comprehension.