Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Get Key with Maximum Value in Python Dictionary

  • 28 Nov, 2025
  • Com 0
How to Get Key with Maximum Value in Python Dictionary

The most common and efficient way to get a key with maximum value in a Python dictionary is to use the max() function with key=dict.get() argument. This method is helpful when you have numeric values in the dictionary.

Getting a Key with Maximum Value in Python Dictionary using max() function with key=dict.get

dict = {"A": 21, "B": 10, "C": 19}

max_key = max(dict, key=dict.get)

print(max_key)  

# Output: A

In this code, key A has a maximum value of 21, which is why it is in the output. We only print the key of that max value. You can also print both the key and the value, depending on your requirements.

It is the cleanest and fastest O(n) Pythonic approach.

Empty dictionary

This happens often when your input dictionary is empty, and you apply the max() function to it. If you are not handling this scenario well, it will throw a ValueError.

empty_dict = {}

max_key = max(empty_dict, key=empty_dict.get)

# ❌ ValueError: max() arg is an empty sequence

To fix the ValueError: max() arg is an empty sequence error, pass default=None to the max() function.

empty_dict = {}

max_key = max(empty_dict, key=empty_dict.get, default=None)

print(max_key)

# Output: None

Multiple keys with the same maximum value

If there is a scenario where there are multiple keys with the same maximum value in a dictionary, the max() function will return the first encountered key.

dict = {"A": 10, "B": 25, "C": 25}

print(max(dict, key=dict.get))

# B (first with value 25)

But what if you want all the keys with maximum values? In that case, you need to use a list comprehension to get the list of keys.

dict = {"A": 10, "B": 25, "C": 25}

max_val = max(dict.values())

max_keys = [k for k, v in dict.items() if v == max_val]

print(max_keys)

# Output: ['B', 'C']

Uncomparable values

What if there is a dictionary with different value types? For example, the first key has an integer value, the second key has a string value, and the third key has a boolean value.

If you apply the max() function on that, it will return TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’ because it cannot compare those values. After all, they are not of the same type.

dict = {"A": 10, "B": "hello", "C": False}

max_keys = max(dict.values())

print(max_keys)

# TypeError: '>' not supported between instances of 'str' and 'int'

Alternate approaches

Approach 1: Using operator.itemgetter()

What if your dictionary is large and contains lots of key-value pairs? In that case, finding the maximum value becomes a lengthy process. That is why you should use the operator.itemgetter() method.

# Large dictionary with 10,000 key-value pairs
# Keys: "key_0", "key_1", ..., "key_9999"
# Values: Random integers between 1 and 1,000,000

import random
from operator import itemgetter

large_dict = {f"key_{i}": random.randint(1, 1_000_000) for i in range(10_000)}

max_key, max_val = max(large_dict.items(), key=itemgetter(1))

print("Max key:", max_key)
# Output: Max key: key_4520

print("Max value:", max_val)
# Output: Max value: 999955

In this code, we used the random module to generate large key-value pairs and find the maximum value.

Approach 2: Using sorted()

Using sorted() method to get the max value from a dictionary

What if your task involves sorting the values and then finding the maximum? That’s where you should use the built-in sorted() function.

dict = {"A": 10, "B": 25, "C": 17}

max_key = sorted(dict.items(), key=lambda x: x[1])[-1][0]

print(max_key)

# Output: B

It gives you both sorted data and the max key.

Approach 3: Using a manual loop

The for loop provides complete control over the dictionary, and we can maintain full transparency into the data to find the maximum value. It is not memory-efficient, but yes, it works well with any data.

dict = {"A": 10, "B": 25, "C": 17}

max_key = None
max_value = float("-inf")

for key, value in dict.items():
    if value > max_value:
        max_value = value
        max_key = key

print(max_key)

# Output: B

That’s all!

Post Views: 3
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Print an Exception in Python
How to Change the Size of Figures in Matplotlib

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend