Python find max value in a dictionary

In this Python tutorial, we will discuss the Python find max value in a dictionary Here we will also cover the below examples:

  • Python find max value in a dictionary of lists
  • Python find highest value in dictionary
  • Python find max value in nested dictionary
  • find max value in dictionary Python without inbuilt function
  • How to find min and max value in dictionary Python
  • Python find max key value in dictionary

Python find max value in a dictionary

  • Let us see how to find a max value in a Python dictionary.
  • By using max() and dict.get() method we can easily get the Key with maximum value in a dictionary.
  • To perform this task there are various methods
    • By using dict.items()
    • By using max() with lambda function
    • By using max() and dict.values()

Examples

Let’s take an example and check how to find a max value in a dictionary by using the max() function

To obtain the maximum value from the dictionary we can use the in-built max() function. In this example, we can use iterable and dict to get the key paired with the maximum value.

Source Code:

my_new_dict = {"q": 18, "z": 10, "o": 13}

fin_max = max(my_new_dict, key=my_new_dict.get)
print("Maximum value:",fin_max)

Here is the Screenshot of the following given code

Python find max value in a dictionary
Python find max value in a dictionary

By using dict.items() method

If you want to iterate key-value pairs in the dictionary then we can use dict.items() method. This method is a built-in function in Python and it will help the user to get the maximum value from the dictionary.

Example:

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))

In the above code first, we will import the operator module and then create a dictionary by using curly brackets{}. Now declare a ‘new_ma_val’ variable and assign max() and key() function as an argument.

Here is the Output of the following given code

Python find max value in a dictionary by using dict
Python find max value in a dictionary by using dict

By using max() and lambda function

In this example, we can use the combination of max() and lambda function to get the values from a dictionary.

Source Code:

Country_dict = {'China':982, 'Egypt':758, 'Malaysia' : 12}

new_val = max(Country_dict, key= lambda x: Country_dict[x])
print("maximum value from dictionary:",new_val)

In the above code, I am using the max() function which uses a lambda expression to return the instance of the ‘country_dict’ variable.

Here is the execution of the following given code

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

By using max() and dict() values

Source Code:

name_dict = {"Oliva": 18, "potter": 56, "Harry": 15}

new_val = name_dict.values()
maximum_val = max(new_val)
print("Maximum value from dict:",maximum_val)

Here is the implementation of the following given code

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 dictionary of lists

  • To find the maximum value in a dictionary of lists we can use the max() function.
  • To solve this problem first we will create a dictionary and assign key-value pair elements. After that create a variable and use the max() function as an argument.

Source Code:

dict_new = {'Micheal': [17,27,81], 'Elijah': [19,61,92]}

new_val = max((max(dict_new[key]) for key in dict_new))
print(new_val)

Here is the Screenshot of the following given code

Python find max value in a dictionary of lists
Python find max value in a dictionary of lists

Read: Python Dictionary to CSV

Python find highest value in dictionary

  • In Python to find the highest value in a dictionary, we can use the concept of key.get() method.
  • By using the built-in max() method. It is provided with the ‘alpha_dict’ variable to obtain the highest value from and to return the key from the given dictionary with the highest value, the dict.get() method is used.

Example:

alpha_dict = {"g": 14, "q": 16, "h": 19}

new_value = max(alpha_dict, key=alpha_dict.get)
print("Highest value from dictionary:",new_value)

Here is the Screenshot of the following given code

Python find highest value in dictionary
Python find the highest value in the dictionary

Read: Python convert dictionary to an array

Python find max value in a nested dictionary

  • To perform this particular task we can use the concept of for loop to iterate each key and get maximum value from the nested dictionary.
  • In this example the inner dictionary “variables” are stored as a string, So after applying the max() function, I would get a set with two values one is key and the other is its corresponding value so you could use element> count_new.

Source Code:

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

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)

In the above code first, we will declare a nested dictionary and assign key-value pair elements in it. Now create a variable and assign an empty dictionary. To check the maximum value in a dictionary we declare a ‘count_new’ variable and set the value as 0. It will check the condition in for loop if the element is greater than 0 and it will iterate all the values and store them into an empty dictionary.

Here is the Output of the following given code.

Python find max value in a nested dictionary
Python find max value in a nested dictionary

Read: Get all values from a dictionary Python

How to find min and max value in dictionary Python

  • In Python to find the minimum and maximum value in a dictionary, we can use the built-in min() and max() function.
  • In Python the max() function is used to find the maximum values in a given dictionary. While in the case of min() function is used to find the minimum values in a dictionary. The values can store either strings or numbers. So in this example, we separate the maximum and minimum values and you will get two numbers.

Example:


your_dictionary = {'Australia':1780, 'England':6723, 'Tokyo': 1946}

new_maximum_val = max(your_dictionary.keys(), key=(lambda new_k: your_dictionary[new_k]))
print('Maximum Value: ',your_dictionary[new_maximum_val])
new_minimum_val = min(your_dictionary.keys(), key=(lambda new_k: your_dictionary[new_k]))
print('Minimum Value: ',your_dictionary[new_minimum_val])

In the above code, I have created a dictionary with different values and keys. Now we have to calculate the minimum and maximum values of the dictionary. To do this task I am using the max() function which uses a lambda expression to return the instance of the ‘your_dictionary’ variable. While in the case of the min() function you can also use the lambda expression to find the minimum value in the Python dictionary.

Here is the execution of the following given code

How to find min and max value in dictionary Python
How to find min and max value in dictionary Python

Read: Python dictionary of tuples

Python find max key value in dictionary

  • Let us see how to find a max key-value pair in a Python dictionary.
  • In Python, the dictionary is used to contain Key-value pairs. If you want to access the values you can use the given keys. In this approach, we will check out the maximum values from the dictionary.

Source Code:

dict_cities = {'Uganda': 763, 'France': 830, 'Tokyo': 193, 'Malaysia': 1682}
max_val = list(dict_cities.values())
max_ke = list(dict_cities.keys())
print(max_ke[max_val.index(max(max_val))])

Here ‘dict_cities’ is the given dictionary and keys hold the list of all dictionary keys. After that, we create a variable ‘max_val’ and assign them a list as an argument to hold the list of all dictionary values by using dict.values() method and in the last line, we are displaying the key of the maximum value.

Here is the Screenshot of the following given code

Python find max key value in dictionary
Python find max key value in a dictionary

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 examples:

  • Python find max value in a dictionary of lists
  • Python find highest value in dictionary
  • Python find max value in nested dictionary
  • find max value in dictionary Python without inbuilt function
  • How to find min and max value in dictionary Python
  • Python find max key value in dictionary