Python Get The Minimum Value In Dictionary with Key

Do you want to get the minimum value in the dictionary with its corresponding key? This article will show you how to get the minimum value in Dictionary in Python.

You can resolve this problem using the min function in Python. The min function will take in two arguments and then it will return the required key that has the minimum value in the dictionary.

Using Min Function in Python To get Minimum Value in Dictionary

The minimum method works similarly for dictionaries as well. All are required to input the dictionary and the keys. Then it will return the key with a minimum value.

Let us see in the below example code the usage of the min function in Python for Dictionary.

#Initializing Small List of Numbers
dictNumber = {'C':3, 'A':1, 'B':2 }

#Finding the key having Minimum Value
keyMin = min(dictNumber, key=dictNumber.get)

#Printing the updated List after Add
print("The key {} is having minimum value {}.".format(keyMin, dictNumber[keyMin]))

Output:

The key A is having minimum value 1.

Using lamba To Get Minimum Value From Dictionary in Python

The above method is not as efficient as it looks. The runtime for a larger dictionary can be high hence to get a minimum value from the dictionary efficiently Lamba should be used.

You should only use lambda when you have very large dictionary data to get the minimum value and key from it.

Let us see in the below example code the usage of lambda in Python for Dictionary to get Minimum Value. This will return the key-value pair directly unlike the one above mentioned.

#Initializing Small List of Numbers
dictNumber = {'C':3, 'A':1, 'B':2 }

#Finding the key having Minimum Value
keyMin = min(dictNumber.items(), key=lambda x: x[1])

#Printing the updated List after Add
print("The key Value pair that is Minimum is {}.".format(keyMin))

Output:

The key Value pair that is Minimum is ('A', 1).
Python Get The Minimum Value In Dictionary with Key

Wrap Up

I hope you were able to get the answer on how to get the minimum value from the dictionary with the key in Python. Let me know if you have any better solution than the one discussed above.

I will be happy to add it here. As you saw I have discussed two methods above out of which one is efficient.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Append Multiple Values To A List in Python
  2. How To Reboot or Restart Python Script
  3. Python: To Check If String Contains a Substring
  4. Python: How To Check If a String Contains a List of Substring
  5. How To Return Null in Python Discussed

Leave a Reply

Your email address will not be published. Required fields are marked *