Many times, we may have problem in which we require to find not just the value, but the corresponding keys to the maximum value in the entire dictionary. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using max() + list comprehension + values() The combination of above functions can be used to perform this particular task. In this, maximum value is extracted using the max function, while values of dictionary is extracted using values(). The list comprehension is used to iterate through the dictionary for matching keys with max value.
Python3
test_dict = { 'Gfg' : 2 , 'for' : 1 , 'CS' : 2 }
print ("The original dictionary is : " + str (test_dict))
temp = max (test_dict.values())
res = [key for key in test_dict if test_dict[key] = = temp]
print ("Keys with maximum values are : " + str (res))
|
Output : The original dictionary is : {'CS': 2, 'Gfg': 2, 'for': 1}
Keys with maximum values are : ['CS', 'Gfg']
Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2 : Using all() + list comprehension This task can also be performed using list comprehension and all function. In this, we take all the elements values, using all function that are smaller than values with keys and return the keys with largest values using list comprehension.
Python3
test_dict = { 'Gfg' : 2 , 'for' : 1 , 'CS' : 2 }
print ("The original dictionary is : " + str (test_dict))
res = [key for key in test_dict if all (test_dict[temp] < = test_dict[key] for temp in test_dict)]
print ("Keys with maximum values are : " + str (res))
|
Output : The original dictionary is : {'CS': 2, 'Gfg': 2, 'for': 1}
Keys with maximum values are : ['CS', 'Gfg']
Method #3 : Using for loop
This method iterates through the values of the dictionary to find the maximum value, and then iterates through the items of the dictionary to find the keys with that maximum value.
Python3
d = { 'CS' : 2 , 'Gfg' : 2 , 'for' : 1 }
max_val = max (d.values())
max_keys = []
for key in d:
if d[key] = = max_val:
max_keys.append(key)
print ( "Keys with maximum values are :" , max_keys)
|
OutputKeys with maximum values are : ['CS', 'Gfg']
Time complexity: O(n)
Auxiliary Space: O(k)
Method #4:Using filter() and lambda()
Algorithm
- Find the maximum value in the dictionary using the max() function.
- Use filter() function with a lambda function to check if the value of each key in the dictionary is equal to the maximum value found in step 1.
- Convert the result of filter() to a list and return the list of keys with maximum values.
Python3
test_dict = { 'Gfg' : 2 , 'for' : 1 , 'CS' : 2 }
print ( "The original dictionary is: " + str (test_dict))
max_val = max (test_dict.values())
res = list ( filter ( lambda x: test_dict[x] = = max_val, test_dict))
print ( "Keys with maximum values are: " + str (res))
|
OutputThe original dictionary is: {'Gfg': 2, 'for': 1, 'CS': 2}
Keys with maximum values are: ['Gfg', 'CS']
Time Complexity:
Finding the maximum value using max() takes O(n) time, where n is the number of items in the dictionary.
Filtering keys with values equal to the maximum value using filter() and a lambda function takes O(n) time, where n is the number of items in the dictionary.
Converting the result of filter() to a list takes O(k) time, where k is the number of items in the result.
Overall, the time complexity of the algorithm is O(n).
Auxiliary Space:
Creating a list of keys with maximum values takes O(k) space, where k is the number of keys with maximum values.
Overall, the space complexity of the algorithm is O(k).