Open In App

Minimum Value Keys in Dictionary – Python

Last Updated : 14 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a dictionary and need to find all the keys that have the minimum value among all key-value pairs. The goal is to identify the smallest value in the dictionary and then collect every key that matches it. For example, in {‘a’: 3, ‘b’: 1, ‘c’: 2, ‘d’: 1}, the minimum value is 1, so the result should be [‘b’, ‘d’]. Let’s explore different methods to achieve this.

Using min()

In this method, we first find the minimum value in the dictionary using the min() function. Once we have that value, we loop through the dictionary and collect all keys that have this minimum value. The logic is straightforward to find the smallest value, then retrieve the keys that match it.

Python
d = {'Gfg': 11, 'for': 2, 'CS': 11, 'geeks': 8, 'nerd': 2}
min_val = min(d.values())

res = [k for k, v in d.items() if v == min_val]
print(res)

Output
['for', 'nerd']

Explanation:

  • min(d.values()) finds the minimum value among the dictionary values.
  • List comprehension collects keys where the value equals the minimum value.

Using defaultdict

Here, we use a defaultdict to group all dictionary keys based on their values. As we loop through each key-value pair, we add the key to a list under its corresponding value. Once everything is grouped, we simply pick the list of keys that belong to the smallest value group. This method is especially useful if you’re also interested in organizing or accessing keys by their values later.

Python
from collections import defaultdict

d = {'Gfg' : 1, 'for' : 2, 'CS' : 1}
g = defaultdict(list)

for k, v in d.items():
    g[v].append(k)

print(g[min(g)])

Output
['Gfg', 'CS']

Explanation:

  • defaultdict(list) initializes a defaultdict where each value is a list.
  • for k, v in d.items() iterates through the dictionary d with key-value pairs.
  • g[v].append(k) appends the key k to the list of values corresponding to v in the defaultdict.

Using itemgetter with groupby

This approach involves sorting the dictionary items by their values first. After sorting, we use groupby to group keys that have the same value. The first group that appears after sorting will always have the minimum value, so we just take the keys from that group. This method is useful when you also care about sorted order or need structured grouping of keys.

Python
from itertools import groupby
from operator import itemgetter

d = {'Gfg': 11, 'for': 2, 'CS': 11, 'geeks': 8, 'nerd': 2}
a = sorted(d.items(), key=itemgetter(1))

res = [k for k, _ in groupby(a, key=itemgetter(1))][0]
res = [k for k, v in a if v == a[0][1]]
print(res)

Output
['for', 'nerd']

Explanation:

  • sorted(d.items(), key=itemgetter(1)) sorts dictionary items by value.
  • groupby(a, key=itemgetter(1)) groups sorted items by their values.
  • [k for k, _ in groupby(a, key=itemgetter(1))][0] extracts the first group of keys (with the minimum value).
  • [k for k, v in a if v == a[0][1]] selects all keys with the minimum value.

Using loop

This is a more manual method where we loop through the dictionary while keeping track of the smallest value seen so far. If we find a value smaller than our current minimum, we reset our result list with that key. If the value matches the current minimum, we add the key to our list. This method gives you full control and doesn’t rely on any built-in functions or imports.

Python
d = {'Gfg' : 1, 'for' : 2, 'CS' : 1}
min_val = float('inf')
res = [] 

for k, v in d.items():
    if v < min_val:
        min_val = v
        res = [k]
    elif v == min_val:
        res.append(k)

print(res)

Output
['Gfg', 'CS']

Explanation:

  • float(‘inf’) initializes min_val to infinity to ensure that the first value encountered will be smaller.
  • for k, v in d.items() iterates through the dictionary items (key-value pairs).
  • if v < min_val checks if the current value is smaller than min_val if true, updates min_val and resets res to [k].
  • elif v == min_val if the current value equals min_val, appends the key k to res.


Next Article
Practice Tags :

Similar Reads