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())

As a result, the output will be:

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"


As a result, the output will be:
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:")
dict_items = myDict.items()
print("Given value is:")
myValue = "WE"
print("Assigned key is:")
myKey = [key for key, value in dict_items if value == myValue]

As a result, the output will be:
The Dictionary is:
{'name': 'WayneEnterprise', 'acronym': 'WE'}
Given value is:
Assigned key is:


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

Like this article? Follow us on Facebook and LinkedIn.