Keys with Maximum value - Python Last Updated : 24 Feb, 2025 Comments Improve Suggest changes Like Article Like Report 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) Output['Gfg', 'CS'] 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) Output['Gfg', 'CS'] 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 loopIn 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) Output['CS', 'Gfg'] 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) Output['Gfg', 'CS'] 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(). Comment More infoAdvertise with us Next Article Keys with Maximum value - Python manjeet_04 Follow Improve Article Tags : Python Python Programs Python dictionary-programs Practice Tags : python 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 Python - Maximum in Row Range Given a range and a Matrix, extract the maximum element out of that range of rows. Input : test_list = [[4, 3, 6], [9, 1, 3], [4, 5, 2], [9, 10, 3], [5, 9, 12], [3, 14, 2]], i, j = 2, 5 Output : 12 Explanation : Checks for rows 2, 3 and 4, maximum element is 12. Input : test_list = [[4, 3, 6], [9, 1 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 Python - Extract Maximum Keys' value dictionaries Given a dictionary, extract all the dictionary which contains a any key which has maximum values among other keys in dictionary list. Input : [{"Gfg" : 3, "is" : 7, "Best" : 8}, {"Gfg" : 9, "is" : 2, "Best" : 9}, {"Gfg" : 5, "is" : 4, "Best" : 10}, {"Gfg" : 3, "is" : 6, "Best" : 14}] Output : [{"Gfg 6 min read Python - Row with Maximum Product We can have an application for finding the lists with the maximum value and print it. This seems quite an easy task and may also be easy to code, but having shorthands to perform the same are always helpful as this kind of problem can come in web development. Method #1 : Using reduce() + lambda The 4 min read Python - Maximum record value key in dictionary Sometimes, while working with dictionary records, we can have a problem in which we need to find the key with maximum value of a particular key of nested records in list. This can have applications in domains such as web development and Machine Learning. Lets discuss certain ways in which this task 6 min read Python - Assign keys with Maximum element index Given Dictionary with value lists, the task is to write a Python program to assign each key with an index of the maximum value in the value list. Examples: Input : test_dict = {"gfg" : [5, 3, 6, 3], "is" : [1, 7, 5, 3], "best" : [9, 1, 3, 5]} Output : {'gfg': 2, 'is': 1, 'best': 0} Explanation : Max 4 min read Like