How to get the key based on the value in a dictionary in python?

Dictionary is a built-in datatype in python that stores the collection of data as key-value pairs. The key-value pairs in the dictionary are enclosed within curly brackets {}. Dict_1 = {1: "one", 2: "two", 5: "five"} represents a dictionary. The key for value "two" in dict_1 is 2.

This article discusses different approaches to get key from the value in a dictionary.

Get key from value by using index() method

Consider a dictionary dict_1 = {1: "red", 2: "black", 3: "green"}. The key for black is 2 in dict_1. The following example illustrates the use of the index() method to get the key from the value in python. The index() is a built-in method that takes an element of the list as an argument and returns the first position at which the element was encountered.

The dict_1.keys() returns a list of keys key_list = [1, 2, 3] and dict_1.values() returns a list of values val_list = ['red', 'black', 'green'] respectively. The val_list.index('black') returns the position of item black in val_list as 1. We use the variable position as an index value to get key from key_list.

dict_1 = {1: "red", 2: "black", 3: "green"}
print("dict_1: ", dict_1)
key_list = list(dict_1.keys())
print("Key_list: ", key_list)
val_list = list(dict_1.values())
print("val_list: ", val_list)
position = val_list.index('black')
print("The key of value black is: ", key_list[position])

The above code returns the output as

dict_1:  {1: 'red', 2: 'black', 3: 'green'}
Key_list:  [1, 2, 3]
val_list:  ['red', 'black', 'green']
The key of value black is:  2

When we try to get the key for a value not present in the dictionary, the val_list.index(value) will raise a ValueError exception. To overcome this problem, we can use an if statement to check if the value is present in val_list.

In the below code we search for the key of 'white' in dict_1. The program returns the output as "Value doesn't exist".

dict_1 = {1: "red", 2: "black", 3: "green"}
print("dict_1: ", dict_1)
key_list = list(dict_1.keys())
print("Key_list: ", key_list)
val_list = list(dict_1.values())
print("val_list: ", val_list)
if 'white' in val_list:
    position = val_list.index('white')
    print("The key of value black is: ", key_list[position])
else:
    print("Value doesn't exist")

Output

dict_1:  {1: 'red', 2: 'black', 3: 'green'}
Key_list:  [1, 2, 3]
val_list:  ['red', 'black', 'green']
Value doesn't exist

Get key from value by using for loop

Consider a dictionary dict_1 = {1: "red", 2: "black", 3: "green"}. A variable flag is initialized to 0. A for loop iterates over the dictionary dict_1. In each iteration, the program checks if the value of the key is "black". If the value is found, the key for the corresponding value is printed and the flag is set to 1.

If the flag remains 0, this indicates that the value doesn't exist in the dictionary.

dict_1 = {1: "red", 2: "black", 3: "green"}
print("dict_1: ", dict_1)
flag = 0
for key in dict_1:
    if dict_1[key] == "black":
        print("The key of value black is: ", key)
        flag = 1 
        break
if flag == 0:
    print("Value doesn't exist")​

The above code returns the output as

dict_1:  {1: 'red', 2: 'black', 3: 'green'}
The key of value black is:  2
Value doesn't exist
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions