Keys with Maximum value - Python
Last Updated : 24 Feb, 2025
In Python, dictionaries are used to store data in key-value pairs and our task is to find the key or keys that have the highest value in a dictionary. For example, in a dictionary that stores the scores of students, you might want to know which student has the highest score. Different methods to identify the key or keys associated with the maximum value are:
Using max() + list comprehension + values()
This approach is a simple and most efficient way to find the key or keys with the highest value in a dictionary. First, we use the max() function to find the highest value in the dictionary. Then, with list comprehension, we can loop through the dictionary and collect all the keys that have this maximum value. The values() method helps to easily get all the values from the dictionary, which we use to find the maximum.
Python d = {'Gfg' : 2, 'for' : 1, 'CS' : 2} temp = max(d.values()) res = [key for key in d if d[key] == temp] print(res)
Explanation:
- max(d.values()) finds the maximum value in the dictionary.
- List comprehension iterates through the keys and adds those with the maximum value to the list res.
- The list res is printed, showing the keys with the maximum value.
Using all() + list comprehension
In this approach, we use list comprehension to loop through the dictionary and check if the value of each key is equal to the maximum value. The all() function is used to verify if all the conditions are met for each key-value pair.
Python d = {'Gfg' : 2, 'for' : 1, 'CS' : 2} res = [key for key in d if all(d[temp] <= d[key] for temp in d)] print(res)
Explanation:
- list comprehension iterates through all keys in the dictionary d.
- For each key, it checks if its value is greater than or equal to all other values in the dictionary using all().
- keys that satisfy this condition are added to the list res.
Using for loop
In this approach, we manually loop through the dictionary and check the value of each key. If the value matches the maximum value, we add the key to a list.
Python 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(max_keys)
Explanation:
- max(d.values()) finds the maximum value in the dictionary d.
- A for loop iterates through all the keys in the dictionary.
- For each key, it checks if its value equals the maximum value and if so, adds the key to the list max_keys.
Using filter() and lambda()
In this approach, we use the filter() function combined with a lambda() function to filter out the keys that have the maximum value. The filter() function checks each key-value pair, and the lambda() function defines the condition (whether the value is equal to the maximum value).
Python d = {'Gfg': 2, 'for': 1, 'CS': 2} max_val = max(d.values()) res = list(filter(lambda x: d[x] == max_val, d)) print(res)
Explanation:
- max(d.values()) finds the maximum value in the dictionary d.
- filter() with a lambda function iterates through the dictionary and checks if the value of each key equals the maximum value.
- keys that meet this condition are collected into a list using list().
Similar Reads
Key with Maximum Unique Values - Python The task involves finding the key whose list contains the most unique values in a dictionary. Each list is converted to a set to remove duplicates and the key with the longest set is identified. For example, in d = {"A": [1, 2, 2], "B": [3, 4, 5, 3], "C": [6, 7, 7, 8]}, key "C" has the most unique v
3 min read
Python | Tuples with maximum key of similar values Sometimes, while working with Python, we can have a problem in which we need to get all the records. This data can have similar values and we need to find maximum key-value pair. This kind of problem can occur while working with data. Let's discuss certain ways in which this task can be done. Method
6 min read
Python - Extract Item with Maximum Tuple Value Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract the item with maximum value of value tuple index. This kind of problem can have application in domains such as web development. Let's discuss certain ways in which this task can be performed. Input :
5 min read
Maximum String Value Length of Key - Python The task of finding the maximum string length of a specific key in a list of dictionaries involves identifying the longest string associated with the given key. Given a list of dictionaries, the goal is to extract the string values of the specified key, compute their lengths and determine the maximu
3 min read
Python - Row with Maximum Record Element Sometimes, while working with Python Records, we can have a problem in which we need to find the row with maximum record element. This kind of problem can come in domains of web development and day-day programming. Let's discuss certain ways in which this task can be performed. Input : test_list = [
7 min read