In this Python tutorial, we will discuss the Python find max value in a dictionary using different methods with examples. I will also explain how to find the maximum value in a dictionary of lists and in a nested dictionary.
To find the maximum value in a Python dictionary, you can use the max() function in several ways. Apply max() directly to dict.values() to get the highest value, or use dict.keys() to find the highest key. Alternatively, employ max(dict, key=lambda k: dict[k]) for a key-based approach, or use import operator and max(dict.items(), key=operator.itemgetter(1)) to get the key-value pair with the highest value.
Python find max value in a dictionary methods
There are four different methods present that can be used with the max() function to Python find max value in a dictionary:
- Operator module with dict.items()
- lambda function
- Using dict.values() function
- dict.keys() function
Let’s see them one by one using some illustrative examples.
Note: The max() function can be used to find the largest element in an iterable like a list, tuple, or string. For more details please read: The max() function in Python.
Method 1: Max value in dictionary Python using operator module with items() function
The items() method of a dictionary in Python returns a view object that displays a list of the dictionary’s key-value tuple pairs.
When used in combination with the operator.itemgetter() function, allows us to sort or retrieve elements based on the value part of these tuples.
Example: Let’s take a Python dictionary and try to find out which key has the highest values.
import operator
max_dict = {'Australia':178, 'Germany':213, 'Japan': 867}
new_ma_val = max(max_dict.items(), key=operator.itemgetter(1))[0]
print((new_ma_val))
Output: We use the operator.itemgetter() method in combination with the max() function to find the key with the maximum value.
Japan
Python find max value in a dictionary using the operator.itemgetter() function with dict.items() function.
Method 2: How to find max value in dictionary Python using the lambda function
When we use the max() function with a lambda function in Python 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 in Python.
Example: Suppose we have a dictionary in Python and we want to find the maximum value holding key in that dictionary.
# 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)
Output: The max() function compares the values and returns the key that has the maximum value.
maximum value from dictionary: China
Python find max value in a dictionary using the lambda function and key parameter.
Method 3: Get max value in dictionary Python using dict.values() function
The most straightforward method is to apply the max() function directly to the values() of the dictionary in Python. This approach returns the maximum value but does not give information about the corresponding key of the Python dictionary.
name_dict = {"Oliva": 18, "potter": 56, "Harry": 15}
maximum_val = max(name_dict.values())
print("Maximum value from dict:",maximum_val)
Output: The implementation of the Python code is mentioned below:
Maximum value from dict: 56
Python find max value in a dictionary using the dict.values() function.
Method 4: Python max dictionary key using dict.keys() function
The keys() function in Python will return the list of the dictionary keys and from that, we can find the maximum of the keys using the max() function in Python.
Example: Suppose we have a dictionary and we have to find the key which is largest among all through Python.
state_populations = {
'CA': 39538223,
'TX': 29145505,
'FL': 21538187,
'NY': 20201249,
'PA': 13002700
}
max_key = max(state_populations.keys())
print('The max of the key in the input dictionary is: ', max_key)
Output: The max() will return the key that is the greatest according to the default comparison rules of Python. For strings, it will be the one that comes last in lexicographical (almost alphabetical) order.
The max of the key in the input dictionary is: TX
Python find max value in a dictionary using the keys() function.
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 nested dictionary.
my_dictionary = {
'Micheal' : {'i' : 15, 'z' : 14},
'George' : {'q' : 2, 'y' : 10, 'w' : 3},
'John' : {'n' : 19, 'm' : 5, 'o' : 2}
}
new_out = {}
for new_k, new_v in my_dictionary.items():
count_new = 0
for element in new_v.values():
if element > count_new:
count_new = element
new_out[new_k] = count_new
print(new_out)
Output: 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, updating the variable if the current value is greater than the previous maximum.
{'Micheal': 15, 'George': 10, 'John': 19}
Python find max value in a dictionary using nested for loop for nested dictionary.
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.
dict_new = {'USA': [17,27,81], 'United Kingdom': [19,61,92]}
new_val = max((max(dict_new[key]) for key in dict_new))
print(new_val)
Output: Here, we are using 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.
This way in Python find max value in a dictionary of lists using list comprehension method.
Conclusion
This article explains how Python find max value in a dictionary using the max() function in four different way like operator.itemgetter() with dict.items() function, using lambda function with key parameter, using dict.values() function, and dict.keys() function. Each method is explained with the help of some illustrative examples.
We have also seen how to find the max value in a nested dictionary, and how to find the max value from a dictionary of lists in Python.
You may also like to read the following tutorials on Python dictionary:
- Python dictionary pop
- Python dictionary get() method
- Python dictionary Copy
- Python dictionary contains
- Python dictionary multiple keys
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.