Python find max value in a dictionary

In this Python tutorial, we will discuss the Python find max value in a dictionary.

To obtain the maximum value from the dictionary, use the in-built max() function of Python.

Here we will discuss the following 3 methods

  • Using dict.items()
  • Using max() and lambda function
  • Using max() and dict()

Python find max value in a dictionary

In this Python section, we are going to discuss different methods for finding the max value in a dictionary.

Method-1: Using dict.items()

Here we will know how to find the maximum value in a dictionary using the dict.items() method:

# Import the operator module
import operator

# Create a dictionary with some key-value pairs
max_dict = {'Australia':178, 'Germany':213, 'Japan': 867}

# Use the operator.itemgetter() method in combination with the max() function to find the key with the maximum value
new_ma_val = max(max_dict.items(), key=operator.itemgetter(1))[0]

# Print the key with the maximum value
print((new_ma_val))

In the above code, we first import the operator module, which provides functions for working with operator overloading.

  • Then we create a dictionary max_dict with key-value pairs. We use the operator.itemgetter() method in combination with the max() function to find the key with the maximum value.
  • The operator.itemgetter() method is used to extract the value from the tuple by using the index passed to it, which is 1 in this case. The max() function compares the values and returns the key-value pair that has the maximum value.
  • The key is extracted from the returned tuple by using [0] indexing and is assigned to the variable new_ma_val. Finally, we print the key with the maximum value which is “Japan” with 867 as the max value.
Python find max value in a dictionary by using dict
Python find max value in a dictionary by using dict

Method-2: Using max() and lambda function

Here we will know how to find the maximum value in a dictionary using the max() and lambda methods:

# Create a dictionary with some key-value pairs
Country_dict = {'China':982, 'Egypt':758, 'Malaysia' : 12}

# Use the max function with a lambda function to find the key with the maximum value
new_val = max(Country_dict, key= lambda x: Country_dict[x])

# Print the key with the maximum value
print("maximum value from dictionary:",new_val)

In the above code, we first create a dictionary Country_dict with key-value pairs.

  • Then we use the max() function with a lambda function to find the key with the maximum value. The lambda function takes a single argument x which is the key of the dictionary and returns the value associated with the key.
  • The max() function then compares the values and returns the key that has the maximum value. Finally, we print the key with the maximum value, which is ‘China’ with 982 as the max value in this case.
Python find max value in a dictionary by using max with lambda function
Python find max value in a dictionary by using max with a lambda function

Method-3: Using max() and dict()

Here we will know how to find the maximum value in a dictionary using the max() and dict() methods:

# Create a dictionary with some key-value pairs
name_dict = {"Oliva": 18, "potter": 56, "Harry": 15}

# Extract the values from the dictionary and assign it to a variable
new_val = name_dict.values()

# Use the max function to find the maximum value
maximum_val = max(new_val)

# Print the maximum value
print("Maximum value from dict:",maximum_val)

In the above code, we first create a dictionary name_dict with key-value pairs.

  • Then we extract the values from the dictionary using the .values() method and assign them to a variable new_val.
  • We then use the max() function to find the maximum value from the extracted values. Finally, we print the maximum value which is 56 in this case.
Python find max value in a dictionary by using max and dict
Python find max value in a dictionary by using max and dict

Read: How to create an empty Python dictionary

Python find max value in a nested dictionary

In this Python section, we will learn how to find the max value in a dictionary within the dictionary.

# Create a nested dictionary 
my_dictionary = {'Micheal' : {'i' : 15, 'z' : 14},
			'George' : {'q' : 2, 'y' : 10, 'w' : 3},
			'John' : {'n' : 19}}

# Create a empty dictionary to store the result
new_out = {}

# Iterate over the outer dictionary keys and values
for new_k, new_v in my_dictionary.items():
    count_new = 0
    # Iterate over the inner dictionary values
    for element in new_v.values():
        # Check if current value is greater than the previous maximum
        if element > count_new:
            count_new = element
    # Assign the maximum value to the outer dictionary key
    new_out[new_k] = count_new

# Print the final dictionary with maximum values
print(new_out)

In the above code, we first create a nested dictionary my_dictionary with multiple keys, each key having its own dictionary as a value.

  • We then create an empty dictionary new_out to store the result. We use a for loop to iterate over the outer dictionary keys and values, and another for loop to iterate over the values of the inner dictionary.
  • We compare each value of the inner dictionary with a variable count_new, updating the variable if the current value is greater than the previous maximum.
  • Then we assign the maximum value to the outer dictionary key by using this key as the key for the new_out dictionary and the maximum value as the value for this key.
Python find max value in a nested dictionary
Python find max value in a nested dictionary

Read: Python Dictionary to CSV

Python find max value in a dictionary of lists

In this Python section, we will discuss how to find the max value in a dictionary containing the list.

# Create a dictionary with some key-value pairs
dict_new = {'USA': [17,27,81], 'United Kingdom': [19,61,92]}

# Use max() and list comprehension to find the maximum value in the dictionary
new_val = max((max(dict_new[key]) for key in dict_new))

# Print the maximum value
print(new_val)

In the above code, we first create a dictionary dict_new with key-value pairs.

  • Then we use the max() function and list comprehension to find the maximum value in the dictionary.
  • The list comprehension iterates over the keys of the dictionary and for each key, it finds the maximum value in the corresponding list using the max() function.
  • Finally, we print the maximum value which is 92 in this case.
Python find max value in a dictionary of lists
Python find max value in a dictionary of lists

You may also like to read the following tutorials on Python dictionary:

In this Python tutorial, we have discussed the Python find max value in a dictionary. Also, we have covered the following methods:

  • Using dict.items()
  • Using max() and lambda function
  • Using max() and dict()