Let’s see how to get the key by value in Python Dictionary.
Method #1: Using list.index()
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.
java python python
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.
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert flattened dictionary into nested dictionary
- Regular Dictionary vs Ordered Dictionary in Python
- Python | Dictionary initialization with common dictionary
- Python | Pretty Print a dictionary with dictionary value
- Python | Convert string dictionary to dictionary
- Python Dictionary
- Python | Get the first key in dictionary
- How to create a Dictionary in Python
- Python Dictionary copy()
- Python | Check if key has Non-None value in dictionary
- Python | Increment value in dictionary
- Python | Add dictionary to tuple
- Python - Extracting Kth Key in Dictionary
- Python | Get first K items in 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 firstname.lastname@example.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.