Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about How to find key by value in python dictionary. so without wasting time lets learn about of this.
How to find key by value in python dictionary
- find key by value in python dictionary
to find key by value in python dictionary just Use dict.items(). By using dict.items() you can find key by value in python dictionary. It is very easy to use. Lets learn about of this by given below example:
mydict={"ahmad":6,"zamrudh":9,"fonil":17,"laksh":10} for key,value in mydict.items(): if value==6: print("this is:",key)
Output :this is: ahmad
- How to find key by value in python dictionary
to find key by value in python dictionary just Use list comprehension. By using list comprehension you can find key by value in python dictionary. It is very easy to use. Lets learn about of this by given below example:
mydict={"ahmad":6,"zamrudh":9,"fonil":17,"laksh":10} t={s for s in mydict if mydict[s]==6} print("this is:",t)
Output :this is: {'ahmad'}
- python find key in dictionary
To find key by value in python dictionary just Use list comprehension. By using list comprehension you can find key by value in python dictionary. It is very easy to use. Lets learn about of this by given below example:
mydict={"ahmad":6,"zamrudh":9,"fonil":17,"laksh":10} t={s for s in mydict if mydict[s]==6} print("this is:",t)
Output :this is: {'ahmad'}
Method 1: Use dict.items()
By using dict.items() you can find key by value in dictionary. It is very easy to use. Lets learn about of this by given below example:
mydict={"ahmad":6,"zamrudh":9,"fonil":17,"laksh":10}
for key,value in mydict.items():
if value==6:
print("this is:",key)
Output :
this is: ahmad
Method 2: Use list comprehension
By using list comprehension you can find key by value from dictionary. It is very easy to use. Lets learn about of this by given below example:
mydict={"ahmad":6,"zamrudh":9,"fonil":17,"laksh":10}
t={s for s in mydict if mydict[s]==6}
print("this is:",t)
Output :
this is: {'ahmad'}
Conclusion
It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?
Also, Read