. Advertisement .
..3..
. Advertisement .
..4..
Computers are mostly used to store information and extract information from it. Python is one of the most powerful programming languages. We can use Python to get computers to do most routine processes. This blog is all about how we can find the key by value in Python dictionary.
How Can You Find the key by value in Python dictionary?
1) Utilize dict.items()
Utilize dict.items() to discover a key by value in a Python dictionary.
Syntax:
dictionary.items()
You can find key by value in a Python dictionary with dict.items(). It’s incredibly simple to use. Let’s look at an example of this:
vardict={"IT":8,"tutoria":9,"cherry":12,"laksh":11}
for key,value in vardict.items():
if value==9:
print("This is ",key)
Result:
This is tutoria
2) Utilize list comprehension
Utilize list comprehension to discover a key by value in a Python dictionary
Note: When using list comprehension you don’t need to write for statement
You can also find key by value in a Python dictionary with list comprehension. It’s incredibly simple to use. Let’s look at an example of this:
vardict={"IT":5,"ITtutoria":10,"kiwi":11,"banana":12}
findKeybyVal={s for s in vardict if vardict[s]==10}
print("This is ", findKeybyVal)
Result:
This is {'ITtutoria'}
Conclusion
Finding the key by value in Python dictionary is a confusing problem. We hope this blog has helped clear the air around how to do it. If you have more questions about this topic, please comment below. Thank you for reading; we are always excited when one of our posts can provide useful information on a topic like this!
Leave a comment