Python
Dictionary
Minimum Value
Key Retrieval
Data Structures

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:

python
1scores = {
2    "alice": 12,
3    "bob": 7,
4    "carol": 15,
5}
6
7lowest_key = min(scores, key=scores.get)
8print(lowest_key)

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:

text
bob

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:

python
min(scores)

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:

python
lowest_key = min(scores, key=lambda k: scores[k])

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():

python
1scores = {
2    "alice": 12,
3    "bob": 7,
4    "carol": 15,
5}
6
7lowest_item = min(scores.items(), key=lambda item: item[1])
8print(lowest_item)

That returns:

text
('bob', 7)

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:

python
1scores = {
2    "alice": 7,
3    "bob": 7,
4    "carol": 15,
5}
6
7print(min(scores, key=scores.get))

If you want all keys with the minimum value, first compute the minimum value, then filter:

python
1scores = {
2    "alice": 7,
3    "bob": 7,
4    "carol": 15,
5}
6
7minimum_value = min(scores.values())
8minimum_keys = [key for key, value in scores.items() if value == minimum_value]
9
10print(minimum_keys)

That gives:

text
['alice', 'bob']

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():

python
1scores = {}
2
3# ValueError: min() arg is an empty sequence
4# lowest_key = min(scores, key=scores.get)

If emptiness is possible, guard it explicitly:

python
1scores = {}
2
3if scores:
4    lowest_key = min(scores, key=scores.get)
5    print(lowest_key)
6else:
7    print("Dictionary is empty")

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.

Course illustration
Course illustration

All Rights Reserved.