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
test_dict = { 'Gfg' : 11 , 'for' : 2 , 'CS' : 11 , 'geeks' : 8 , 'nerd' : 2 }
print ("The original dictionary is : " + str (test_dict))
temp = min (test_dict.values())
res = [key for key in test_dict if test_dict[key] = = temp]
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
test_dict = { 'Gfg' : 1 , 'for' : 2 , 'CS' : 1 }
print ("The original dictionary is : " + str (test_dict))
res = [key for key in test_dict if
all (test_dict[temp] > = test_dict[key]
for temp in test_dict)]
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
test_dict = { 'Gfg' : 11 , 'for' : 2 , 'CS' : 11 , 'geeks' : 8 , 'nerd' : 2 }
print ( "The original dictionary is : " + str (test_dict))
temp = min (test_dict.values())
res = []
for key, value in test_dict.items():
if (value = = temp):
res.append(key)
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
test_dict = { 'Gfg' : 11 , 'for' : 2 , 'CS' : 11 , 'geeks' : 8 , 'nerd' : 2 }
print ( "The original dictionary is : " + str (test_dict))
res = {key:val for key,val in test_dict.items() if val = = min (test_dict.values())}
print ( "Keys with minimum values are : " + str (res.keys()))
|
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:
- Initialize a variable min_val to the maximum possible value that can be stored in an integer variable.
- Initialize an empty list min_keys to store the keys with the minimum values.
- Iterate through the dictionary using a for loop.
- For each key-value pair, check if the value is less than the current value of min_val.
- If yes, update the value of min_val to the current value of the key, and clear the list min_keys.
- Append the current key to the list min_keys if its value is equal to min_val.
- Finally, print the list min_keys.
Below is the implementation of the above approach:
Python3
test_dict = { 'Gfg' : 11 , 'for' : 2 , 'CS' : 11 , 'geeks' : 8 , 'nerd' : 2 }
print ( "The original dictionary is : " + str (test_dict))
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)
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.
Last Updated :
13 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...