How To Get the Key With a Maximum Value in a Dictionary in Python – Definitive Guide

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.

Basic Example

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

This tutorial teaches you the methods available to get the key with a maximum value in a dictionary.

To find the maximum value in a dictionary in Python, read: How To Find The Maximum Value in A Dictionary in Python

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.

  • iterableMandatory – an iterable from which the maximum value needs to be identified
  • keyOptional – Argument specifying an ordering function. Here, the value of each key is ordered, and the maximum value’s key is returned when you pass dict.get
  • defaultOptional – 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

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

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,

  1. yourdict.items() – Returns both key and value to iterator
  2. key = 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 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, (‘four’, 4). You can use the index -> [0] to find the key.

Code

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,

  1. Find the maximum value in the dictionary using max(yourdict.values())
  2. Using list comprehension, iterate over the dictionary items and check if the value is equal to the identified maximum value.
  3. 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

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,

  • Use the dict.values() method to the max() function.
  • The dict.values() method returns the values of all keys as a list
  • 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

Additional Resources

Leave a Comment