Let’s see how to get the key by value in Python Dictionary.
Method #1: Using list.index()
The index() method returns index of corresponding value in a list. Below is an implementation how to use index() method to fetch Dictionary key using value.
# creating a new dictionary 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_list[val_list.index(100)]) print(key_list[val_list.index(112)]) # one-liner print(list(my_dict.keys())[list(my_dict.values()).index(112)]) |
java python python
Explanation:
The approach used here is to find two separate lists of keys and values. Then fetch the key using the position of the value in the val_list. As key at any position N in key_list will have corresponding value at position N in val_list.
Method #2: Using dict.item()
We can also fetch key from a value by matching all the values and then print the corresponding key to given value.
# function to return key for any value def get_key(val): for key, value in my_dict.items(): if val == value: return key return "key doesn't exist" # Driver Code my_dict ={"java":100, "python":112, "c":11} print(get_key(100)) print(get_key(11)) |
java c
Recommended Posts:
- Python - Extract Key's Value, if Key Present in List and Dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python | Get key with maximum value in Dictionary
- Python | Get first N key:value pairs in given dictionary
- Python - Extract ith Key's Value of K's Maximum value dictionary
- Python - Filter key's value from other key
- Python | Get the first key in dictionary
- Python - Get next key in Dictionary
- Python | Extract key-value of dictionary in variables
- Python | Accessing Key-value in Dictionary
- Add a key:value pair to dictionary in Python
- Python | Convert byteString key:value pair of dictionary to String
- Python | Convert key-value pair comma separated string into dictionary
- Python | Check if key has Non-None value in dictionary
- Python | Value summation of key in dictionary
- Python - Check for Key in Dictionary Value list
- Python - Convert Key-Value list Dictionary to List of Lists
- Python - Maximum record value key in dictionary
- Python - Key Value list pairings in Dictionary
- Python - Convert key-value String to dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

