Get the key corresponding to the minimum value within a dictionary
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, the cleanest way to get the key for the smallest value in a dictionary is to use min() with a custom key function. The operation is straightforward, but there are a few important details around empty dictionaries, ties, and whether you want one key or all keys sharing the minimum value.
Use min() with the dictionary's values
The most common pattern is:
This works because min() iterates over the dictionary keys, and scores.get tells Python to compare them by their corresponding values.
In this example, the result is:
This is usually the best answer because it is short, idiomatic, and easy to read.
Understand what min() is really doing
A dictionary by itself iterates over keys, not key-value pairs. So:
would compare the keys alphabetically, not the numeric values. The key=scores.get argument changes the comparison rule so min() asks, "Which key has the smallest mapped value?"
You can write the same thing more explicitly with a lambda:
Both forms are valid. scores.get is just the simpler version when all you need is a direct lookup.
If you need the key and value together
Sometimes you want the whole key-value pair instead of only the key. In that case, use items():
That returns:
This version is helpful when the next step needs both pieces of information and you do not want to perform a second lookup.
Handle ties if several keys share the minimum
min() returns only one result. If several keys have the same lowest value, Python returns the first one it encounters according to dictionary iteration order.
For example:
If you want all keys with the minimum value, first compute the minimum value, then filter:
That gives:
This is the right solution when ties actually matter in your application.
Guard against an empty dictionary
An empty dictionary raises ValueError when passed to min():
If emptiness is possible, guard it explicitly:
That is better than letting the error happen accidentally unless the exception is exactly what you want.
Complexity and practicality
Finding the minimum value key requires scanning the dictionary once, so the time complexity is O(n). That is already optimal for an unsorted mapping because Python has to inspect the values to know which one is smallest.
In other words, the idiomatic min(..., key=...) solution is not just readable. It is also the correct performance shape for the problem.
Common Pitfalls
The biggest mistake is calling min(dictionary) and expecting Python to compare values. Without a custom key function, Python compares dictionary keys.
Another common issue is ignoring ties. min() returns one key, not all keys with the same minimum value.
People also forget that empty dictionaries raise ValueError. Add an explicit guard if empty input is possible.
Finally, if you already need both the key and the value, using min(dictionary.items(), key=lambda item: item[1]) is often cleaner than finding the key first and then looking the value up separately.
Summary
- Use
min(my_dict, key=my_dict.get)to get the key with the smallest value. - Use
min(my_dict.items(), key=lambda item: item[1])when you need the key and value together. - If several keys share the minimum value, filter the dictionary after finding the minimum value.
- Guard against empty dictionaries before calling
min(). - Remember that plain
min(dictionary)compares keys, not values.

