close

How to find key by value in python dictionary

Hi guys. How are you all? I hope you all fine. Today in this tutorial we will learn about How to find key by value in python dictionary. Dictionary are made by the pairing of the key and value. Here we will learn to find the key by value. There are some methods by using them we can find the key of dictionary with the use of value. So without wasting your time lets learn about of it.

How to find key by value in python dictionary

  1. find key by value in python dictionary

    to find key by value in python dictionary just Use dict.items(). You can find the key of dictionary by using the value of it in python. Here for this we will use dict.items(). And we will use the for loop for that. First of all we will make a dictionary and then by using dict.items and the value of the key we will find the key in python. So lets learn this by given below example: mydict ={"vruksha":35, "henny":26, "Roshan":83} def mykey(val): for key, value in mydict.items(): if val == value: return key return "There is no such Key" print(mykey(35)) print(mykey(83)) print(mykey(3)) Output : vruksha Roshan There is no such Key

  2. How to find key by value in python dictionary

    to find key by value in python dictionary just Use .values() and .keys(). By using .values() and.keys() you can find out the key of the dictionary in python. Here first of all make your dictionary and then dict.keys() function and then use dict.values() function and print it. It will give you the key of the dictionary. Lets learn this by given below example: mydict ={"vruksha":35, "henny":26, "Roshan":83} mykey = list(mydict.keys()) myvalue = list(mydict.values()) var1 = myvalue.index(35) print(mykey[var1]) var1 = myvalue.index(26) print(mykey[var1])
    Output : vruksha henny

Method 1: Use dict.items()

You can find the key of dictionary by using the value of it in python. Here for this we will use dict.items(). And we will use the for loop for that. First of all we will make a dictionary and then by using dict.items and the value of the key we will find the key in python. So lets learn this by given below example:

mydict ={"vruksha":35, "henny":26, "Roshan":83}
def mykey(val):
    for key, value in mydict.items():
         if val == value:
             return key
    return "There is no such Key"
print(mykey(35))
print(mykey(83))
print(mykey(3))

Output :

vruksha
Roshan
There is no such Key

Method 2: Use .values() and .keys()

By using .values() and.keys() you can find out the key of the dictionary in python. Here first of all make your dictionary and then dict.keys() function and then use dict.values() function and print it. It will give you the key of the dictionary. Lets learn this by given below example:

mydict ={"vruksha":35, "henny":26, "Roshan":83}
mykey = list(mydict.keys())
myvalue = list(mydict.values())
var1 = myvalue.index(35)
print(mykey[var1])
var1 = myvalue.index(26)
print(mykey[var1])

Output :

vruksha
henny

Method 3: Use list.index()

Here we will use list.index() function for to find the key of the dictionary in python. It is very easy and simple method. Here you have to put the values of the dictionary which you want to find the key of in just .index() and it will give you the key of it. So lets learn this.

mydict ={"vruksha":35, "henny":26, "Roshan":83}
key_list = list(mydict.keys())
val_list = list(mydict.values())
position = val_list.index(35)
print(key_list[position])
position = val_list.index(83)
print(key_list[position])
print(list(mydict.keys())[list(mydict.values()).index(83)])

Output :

vruksha
Roshan
Roshan

Conclusion

Hope all 6 Method Are Useful For You. Comment Below Which Method You Used To Convert String To Datetime In Python. Thank You.

Also Read

Leave a Comment