How To Find The Maximum Value in A Dictionary in Python – With Examples

Python dictionaries allow you to store items as key-value pairs.

You can find the maximum value in a dictionary in Python using the max(yourdict.values()) statement.

This tutorial teaches you how to find the maximum value in a dictionary in Python using the different methods and when it is appropriate to use each method.

If You’re in Hurry…

You can use the max() function and pass the list of dictionary values using the yourdict.values() method, as demonstrated in the following code.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
    "five": 5
}

maximum_value = max(yourdict.values())

maximum_value

Output

    4

If You Want to Understand Details, Read on…

There are different methods to find the max value from a dictionary in Python.

Let us learn each method in detail.

If you need the key of the maximum value in the dictionary, read: – How To Get the Key With a Maximum Value in a Dictionary in Python

Using Max Function and Dict.values

The max() function returns the largest item in the iterable.

It accepts one mandatory parameter and two optional parameters.

  • iterableMandatory – an iterable from which the maximum value needs to be identified

You can pass the list of dictionary values to the max function using the dict.values() method.

From the list of values passed, the max() function returns the maximum value.

You can use this method when you want to find the maximum value in a dictionary and do not need the key information of the maximum value.

Code

The following code demonstrates how to find the maximum value in a dictionary using the max() function.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
    "five": 5
}

maximum_value = max(yourdict.values())

maximum_value

Output

    4

Using Max Function and Operator.itemgetter

This section teaches you how to use the operator module’s itemgetter to get the maximum value in a dictionary in Python.

The itemgetter() method returns a callable object that fetches an item from its operand.

Finding the maximum value using the itemgetter() works as explained below:

In the max() function,

  1. yourdict.items() – Returns both key and value to the iterator
  2. key = operator.itemgetter(1) – Specifies that the sorting function to find the maximum value should use the value in index 1. For example – a tuple with key and value will be available during iteration. Key is available in index 0, and value is available in index 1. When you pass 1 to the itemgetter(), the item in index 1 will be returned to the calling function. This means the value will be used to find the largest item
  3. After finding the largest value, the max() function returns the tuple. For example, (‘five’, 5). You can use the index -> [1] to get the value from the tuple

You can use this method when you want to find the maximum value in a dictionary and need the key of the maximum value.

Code

The following code demonstrates how to find the maximum value in a dictionary in python using the max() function and the itemgetter to fetch both the maximum value and its key.

import operator

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
    "five": 5
}

maximum_value_pair = max(yourdict.items(), key = operator.itemgetter(1))

maximum_value = maximum_value_pair[1]

maximum_value

Output

    5

Get All Keys With Max Value

You may need to get all keys with a maximum value in a dictionary to find if more than one key contains the maximum value.

  • Find the maximum value in a dictionary
  • Using list comprehension, find all the keys with that specific value

Code

The following code demonstrates how to get all the keys with the maximum value in a 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']

Conclusion

You’ve learned how to find the maximum value in a dictionary in Python.

You can also use these methods to return the maximum value in a dictionary in python to any other calling function.

You May Also Like

Leave a Comment