In Python, we can get key from value in a dictionary by using certain methods like dict.items()
, .keys()
, .values()
, etc.
In Python, a dictionary is a collection of items in a key-value pair. The items stored in a dictionary are generally in an unordered manner. In a dictionary, each key is paired with a value.
For example, the key “Ball” is paired with the value 2 in {“Apple”: 1, “Ball”: 2}. In this article, we aim to get the value of the key when we know the value of the element.
Python dict get key from value | Using index and values
We can use the index() and values() functions of dictionary collection.
For example,
dictA = {"Apple": 3, "Ball": 11, "Cat": 8}
keys = list(dictA.keys())
vals = list(dictA.values())
print(keys[vals.index(11)])
print(keys[vals.index(8)])
As a result, the output will be:
Ball
Cat
Here, we create a list to first get the values and then the keys from it.
Using Items method to get key from value python
In this example, we use the items method in a dictionary to get keys from values.
dictA = {"Apple": 3, "Ball": 16, "Cat": 8}
def GetKey(val):
for key, value in dictA.items():
if val == value:
return key
return "key doesn't exist"
print(GetKey(16))
print(GetKey(3))
print(GetKey(10))
As a result, the output will be:
Ball
Apple
key doesn't exist
Here, we create a function GetKey() to take the value as input and compare it with the value present in each item of the dictionary. If the value matches the key is returned.
Using the get() method
The get() method returns the value for the specific key if the key is in the dictionary.
The syntax of using get() method is:
dict.get(key[, value])
For example,
person = {'name': 'Bruce', 'age': 47}
print('Name: ', person.get('name'))
print('Age: ', person.get('age'))
print('Salary: ', person.get('salary')) # when value is not provided
print('Salary: ', person.get('salary', 100000.0)) # when value is provided
As a result, the output will be:
Name: Bruce
Age: 47
Salary: None
Salary: 100000.0
Here, we take the name, age and salary of an individual in a dictionary. After that, we print it using the get method.
Get key from a value by using list comprehension in Python
In place of using the index() method, we can use list comprehension to get the keys related to the given value. In this example, we create a list of keys that have assigned values equal to the given value using list comprehension.
myDict = {"name": "WayneEnterprise", "acronym": "WE"}
print("The Dictionary is:")
print(myDict)
dict_items = myDict.items()
print("Given value is:")
myValue = "WE"
print(myValue)
print("Assigned key is:")
myKey = [key for key, value in dict_items if value == myValue]
print(myKey)
As a result, the output will be:
The Dictionary is:
{'name': 'WayneEnterprise', 'acronym': 'WE'}
Given value is:
WE
Assigned key is:
['acronym']
Conclusion
To sum it up, we have learnt different ways to find key by value in python dictionaries using various methods. However, there isn’t any particular method to extract a key assigned with the value when we have multiple values.
You can also learn about Python timeit