How to find a key by its value in a Dictionary Python - Brown Surfing

How to find a key by its value in a Dictionary Python

Posted on: January 24th, 2023
By: Efrain Lemus

Here’s a good article where I found several solutions:
https://www.geeksforgeeks.org/python-get-key-from-value-in-dictionary/

The one below is the one that made sense for me.

my_dict ={"java":100, "python":112, "c":11}
 
# list out keys and values separately
key_list = list(my_dict.keys())
val_list = list(my_dict.values())
 
# print key with val 100
position = val_list.index(100)
print(key_list[position])

Have any questions or comments? Write them below!


Leave a Reply

Your email address will not be published. Required fields are marked *