Open In App

Python | Minimum value keys in Dictionary

Many times, we may have problem in which we require to find not just the value, but the corresponding keys to the minimum value in the entire dictionary. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using min() + list comprehension + values() The combination of above functions can be used to perform this particular task. In this, minimum value is extracted using the min function, while values of dictionary is extracted using values(). The list comprehension is used to iterate through the dictionary for matching keys with min value. 






# Python3 code to demonstrate working of
# Finding min value keys in dictionary
# Using min() + list comprehension + values()
 
# initializing dictionary
test_dict = {'Gfg' : 11, 'for' : 2, 'CS' : 11, 'geeks':8, 'nerd':2}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Using min() + list comprehension + values()
# Finding min value keys in dictionary
temp = min(test_dict.values())
res = [key for key in test_dict if test_dict[key] == temp]
 
# printing result
print("Keys with minimum values are : " + str(res))

Output:

The original dictionary is : {‘nerd’: 2, ‘Gfg’: 11, ‘geeks’: 8, ‘CS’: 11, ‘for’: 2} Keys with minimum values are : [‘nerd’, ‘for’]



Time Complexity: O(N)
Auxiliary Space: O(N)

Method #2 : Using all() + list comprehension This task can also be performed using list comprehension and all function. In this, we take all the elements values, using all function that are greater than values with keys and return the keys with smallest values using list comprehension. 




# Python3 code to demonstrate working of
# Finding min value keys in dictionary
# Using all() + list comprehension
 
# initializing dictionary
test_dict = {'Gfg' : 1, 'for' : 2, 'CS' : 1}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Using all() + list comprehension
# Finding min value keys in dictionary
res =  [key for key in test_dict if
        all(test_dict[temp] >= test_dict[key]
        for temp in test_dict)]
 
# printing result
print("Keys with minimum values are : " + str(res))

Output:

The original dictionary is : {‘Gfg’: 1, ‘CS’: 1, ‘for’: 2} Keys with minimum values are : [‘Gfg’, ‘CS’]

Time complexity: O(n^2) – where n is the number of keys in the dictionary. This is because the code iterates through the dictionary twice (once for each key) for each key.
Auxiliary space: O(n) – the space used by the list comprehension to store the keys with minimum values.

Method #3 : Using items() function




# Python3 code to demonstrate working of
# Finding min value keys in dictionary
 
# initializing dictionary
test_dict = {'Gfg': 11, 'for': 2, 'CS': 11, 'geeks': 8, 'nerd': 2}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Finding min value keys in dictionary
temp = min(test_dict.values())
res = []
for key, value in test_dict.items():
    if(value == temp):
        res.append(key)
 
# printing result
print("Keys with minimum values are : " + str(res))

Output
The original dictionary is : {'Gfg': 11, 'for': 2, 'CS': 11, 'geeks': 8, 'nerd': 2}
Keys with minimum values are : ['for', 'nerd']

Time Complexity:O(N)
Auxiliary Space: O(N)

Method #4 : Using min() + dictionary comprehension




# Python3 code to demonstrate working of
# Finding min value keys in dictionary
# Using min() + dictionary comprehension
   
# initializing dictionary
test_dict = {'Gfg' : 11, 'for' : 2, 'CS' : 11, 'geeks':8, 'nerd':2}
   
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
   
# Using min() + dictionary comprehension
# Finding min value keys in dictionary
res = {key:val for key,val in test_dict.items() if val == min(test_dict.values())}
   
# printing result
print("Keys with minimum values are : " + str(res.keys()))
#This code is contributed by Edula Vinay Kumar Reddy

Output
The original dictionary is : {'Gfg': 11, 'for': 2, 'CS': 11, 'geeks': 8, 'nerd': 2}
Keys with minimum values are : dict_keys(['for', 'nerd'])

Time Complexity: O(N)
Auxiliary Space: O(N)

Method #5: Using a loop

Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Finding min value keys in dictionary
# Using a loop
 
# initializing dictionary
test_dict = {'Gfg': 11, 'for': 2, 'CS': 11, 'geeks': 8, 'nerd': 2}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Using a loop
# Finding min value keys in dictionary
min_val = float('inf')
min_keys = []
for key, val in test_dict.items():
    if val < min_val:
        min_val = val
        min_keys.clear()
    if val == min_val:
        min_keys.append(key)
 
# printing result
print("Keys with minimum values are : " + str(min_keys))

Output
The original dictionary is : {'Gfg': 11, 'for': 2, 'CS': 11, 'geeks': 8, 'nerd': 2}
Keys with minimum values are : ['for', 'nerd']

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(k), where k is the number of keys with the minimum values.


Article Tags :