Open In App

Python | Keys with Maximum value

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

 `# Python3 code to demonstrate working of``# Keys with Maximum value``# Using max() + list comprehension + values()` `# initializing dictionary``test_dict ``=` `{``'Gfg'` `: ``2``, ``'for'` `: ``1``, ``'CS'` `: ``2``}` `# printing original dictionary``print``("The original dictionary ``is` `: " ``+` `str``(test_dict))` `# Using max() + list comprehension + values()``# Keys with Maximum value``temp ``=` `max``(test_dict.values())``res ``=` `[key ``for` `key ``in` `test_dict ``if` `test_dict[key] ``=``=` `temp]` `# printing result``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

 `# Python3 code to demonstrate working of``# Keys with Maximum value``# Using all() + list comprehension` `# initializing dictionary``test_dict ``=` `{``'Gfg'` `: ``2``, ``'for'` `: ``1``, ``'CS'` `: ``2``}` `# printing original dictionary``print``("The original dictionary ``is` `: " ``+` `str``(test_dict))` `# Using all() + list comprehension``# Keys with Maximum value``res ``=` `[key ``for` `key ``in` `test_dict ``if` `all``(test_dict[temp] <``=` `test_dict[key] ``for` `temp ``in` `test_dict)]` `# printing result``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

 `# create a dictionary``d ``=` `{``'CS'``: ``2``, ``'Gfg'``: ``2``, ``'for'``: ``1``}` `# get the maximum value in the dictionary``max_val ``=` `max``(d.values())` `# create a list to store the keys with maximum values``max_keys ``=` `[]` `# loop through the dictionary``for` `key ``in` `d:` `    ``# check if the value of the current key is equal to the maximum value``    ``if` `d[key] ``=``=` `max_val:``        ` `        ``# append the key to the list of max keys``        ``max_keys.append(key)` `# print the keys with maximum values``print``(``"Keys with maximum values are :"``, max_keys)`

Output

`Keys with maximum values are : ['CS', 'Gfg']`

Time complexity: O(n)

Auxiliary Space: O(k)

Method #4:Using filter() and lambda()

Algorithm

1. Find the maximum value in the dictionary using the max() function.
2. 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.
3. Convert the result of filter() to a list and return the list of keys with maximum values.

Python3

 `# initializing dictionary``test_dict ``=` `{``'Gfg'``: ``2``, ``'for'``: ``1``, ``'CS'``: ``2``}` `# printing original dictionary``print``(``"The original dictionary is: "` `+` `str``(test_dict))` `# using filter() to find keys with maximum value``max_val ``=` `max``(test_dict.values())``res ``=` `list``(``filter``(``lambda` `x: test_dict[x] ``=``=` `max_val, test_dict))` `# printing result``print``(``"Keys with maximum values are: "` `+` `str``(res))``#This code is contributed by Vinay Pinjala.`

Output

```The 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).