In python, we can get the values present in a dictionary using the keys by simply using the syntax dict_name[key_name]. But there isn’t any method to extract a key associated with the value when we have values. In this article, we will see the ways with help of which we can get the key from a given value in a dictionary.
Get key from a value by searching the items in a dictionary
This is the simplest way to get a key for a value. In this method we will check each key-value pair to find the key associated with the present value.For this task, we will use the items() method for a python dictionary. The items() method returns a list of tuples containing key value pairs. We will search each tuple to find the key associated with our value as follows.
myDict={"name":"PythonForBeginners","acronym":"PFB"}
print("Dictionary is:")
print(myDict)
dict_items=myDict.items()
print("Given value is:")
myValue="PFB"
print(myValue)
print("Associated Key is:")
for key,value in dict_items:
if value==myValue:
print(key)
Output:
Dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB'}
Given value is:
PFB
Associated Key is:
acronym
In the above program, we have created a list of items in myDict using myDict.items() and then we check for each item in the list to find the key for our value.
Get key from a value by using python lists
We can create a separate list of keys and values and then we can retrieve the keys from the given value using index() method. For this task, we will first create a list of keys present in the dictionary using keys() method and then we will create the list of values present in the dictionary using values() method. Now we will get the index of the given value from the list of values using index() method. As we know that the list of keys has the same order of keys as values in the list of values, the index of the values in the list of values will be the same as the index of the associated key in the list of keys. So, after finding the index of the value in the list of values, we can find the key in the list of keys at the same index. This can be done as follows.
myDict={"name":"PythonForBeginners","acronym":"PFB"}
print("Dictionary is:")
print(myDict)
dict_keys=list(myDict.keys())
dict_values=list(myDict.values())
print("Given value is:")
myValue="PFB"
print(myValue)
val_index=dict_values.index(myValue)
print("Associated key is:")
myKey=dict_keys[val_index]
print(myKey)
Output:
Dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB'}
Given value is:
PFB
Associated key is:
acronym
Get key from a value by using list comprehension
Instead of using index() method, we can use list comprehension to get the keys associated with the given value. To find the key, we will create a list of keys which have associated values equal to the given value using list comprehension as follows.
myDict={"name":"PythonForBeginners","acronym":"PFB"}
print("Dictionary is:")
print(myDict)
dict_items=myDict.items()
print("Given value is:")
myValue="PFB"
print(myValue)
print("Associated key is:")
myKey=[key for key,value in dict_items if value==myValue]
print(myKey)
Output:
Dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB'}
Given value is:
PFB
Associated key is:
['acronym']
Conclusion
In this article, we have seen three ways to obtain a key from a python dictionary using list comprehension, items() method and list index() method. Stay tuned for more informative articles.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.