Find all keys with maximum value in python dictionary

Python Dictionary

In this post, we are going to understand how to find all keys with maximum value in the python dictionary. Dictionary is an ordered collection of data in the form of key-value pair. The key in the dictionary is unique but the value with the key can be duplicated. So often we have multiple values in the dictionary that has the same or maximum value. While data manipulation we have to Find all keys with maximum value in python dictionary.

max() with get to a keys with maximum value

In this code example, we are using the max() method it returns the largest item from iterable(dictionary, tuple, list) in python. It is returning the very first max value key from the dictionary.

Code Example

Let us understand this with the help of an code example:

#Program get a single key with maximum value in a python dictionary

stu_dict = {'Score':200,'age':20,'salary':1000,'sal':1000}

maximum = max(stu_dict, key=stu_dict.get) 

print('maximum values keys in dictionary =\n',maximum)

Output

maximum values keys in dictionary =
 salary

List comprehension to Find all key with maximum values

In this code example, we are using the list comprehension to find all keys with maximum values. Firstly we are finding the key with max value (max_value) from the dictionary using the max() method and later we are iterating over the dictionary comparing each key-value with (max_value) and match with max values, this is how it returns all the keys that match with this max value.

Code Example

Let us understand this with the below code example and try to make our understanding more stronger.

#Program find all key with maximum value in a python dictionary

stu_dict = {'Score':200,'age':20,'salary':1000,'sal':1000}

max_value = max(stu_dict.values())

keys_with_max_val = [key for key,val in stu_dict.items() if val == max_value]

print('keys with maximum values in dictionary =\n',keys_with_max_val)

Output

keys with maximum values in dictionary =
 ['salary', 'sal']

Lambada to Get all key with maximum values

In this code example, we are using the lambada with max() method to find all the keys with maximum value from a dictionary. Firstly we find the first key with max value (max_val) and then we are looping over the elements of the dictionary checking each key-value with max value. If the value is matching then we are appending in the list (keys_list).

Code Example

Here is the code example to understand the concept with practical.

#Program find all key with maximum value in a python dictionary

stu_dict = {'Score':200,'age':20,'salary':1000,'sal':1000}

#max  key and value pair
max_val = max(stu_dict.items(), key=lambda x: x[1])

keys_list = list()

#looping over the dictionary items

for key, value in stu_dict.items():

    if value == max_val[1]:

      #appending key into list
      
        keys_list.append(key)

print('keys with maximum values in dictionary =\n',keys_list)

Output

keys with maximum values in dictionary =
 ['salary', 'sal']

Get all key with maximum values

In this code example, we are using the list comprehension to find all keys with maximum values. Firstly we are finding the key with max value (max_value) from the dictionary using the max() method and later we are iterating over the dictionary comparing each key-value with (max_value) .

Code Example

Let us understand this with the below code sample.

#Program find all key with maximum value in a python dictionary

stu_dict = {'Score':200,'age':20,'salary':1000,'sal':1000}

#max  key and value pair
max_val = max(stu_dict.items(), key=lambda x: x[1])

max_list =[item[0] for item in stu_dict.items() if item[1]==max_val[1]] 

print('keys with maximum values in dictionary :',max_list)

Output

keys with maximum values in dictionary: ['salary', 'sal']

Conclusion:

In this post, we have understood different ways to Find all keys with maximum value in python dictionary. Now we can easily find single or all keys with maximum values in a dictionary by using all the above ways.

We hope you will find this post helpful.

Happy Learning!!