Get key by value in dictionary
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Understanding How To Get Key By Value in a Dictionary
In Python, dictionaries are versatile data structures that are used to store data values in key-value pairs. Dictionaries are unordered, changeable, and do not allow duplicate keys. While retrieving a value by its key is straightforward, occasionally you may want to retrieve a key based on its value. This might not be as direct since dictionaries are primarily designed for fast key-to-value lookups. Fortunately, there are several ways you can achieve this.
Basics of Dictionary in Python
A dictionary in Python is declared using curly braces {} and consists of key-value pairs. Here's a basic example:
Here, 'apple', 'carrot', and 'chicken' are keys, and 'fruit', 'vegetable', and 'meat' are the respective values.
Why Retrieve Keys by Value?
Sometimes, it may be necessary to reverse this relationship to find out which key(s) correspond to a particular value. This could be useful in various contexts such as database lookups, inverse dictionaries, or merely for analysis purposes.
Methods to Get Key by Value
Method 1: Loop Through the Dictionary
The simplest way to find a key by its value is to loop through the dictionary items and check for matches.
Pros:
- Straightforward.
- Easy to understand.
Cons:
- Relatively slow for large dictionaries because it involves checking each key-value pair.
Method 2: Dictionary Comprehension
You can make the loop more succinct using dictionary comprehension.
Pros:
- More Pythonic and concise.
Cons:
- It still performs a linear search through the dictionary.
Method 3: Using Inverted Dictionary
If you need to perform multiple reverse lookups, it can be efficient to create an inverted dictionary where the values become keys and vice versa. However, this method is viable only if the values are unique.
Pros:
- Fast O(1) lookup once the dictionary is inverted.
Cons:
- Memory overhead with the additional dictionary.
- Assumes values are unique, which may not always be guaranteed.
Handling Non-Unique Values
If your dictionary contains non-unique values and you want to get all the keys associated with a particular value, you can modify the approach slightly:
Key Points Table
| Concept | Explanation |
| Access Dictionary | Direct access using d[key] or d.get(key). |
| Reverse Lookup | Finding keys by value using loops or comprehension. |
| Inverted Dictionary | Swap keys and values for a new dictionary for fast lookups. |
| Handling Duplicates | Custom functions to handle and list keys for non-unique values. |
Additional Considerations
- Performance: Remember that searching by value does not leverage the dictionary's hashed lookup features, resulting in time complexity of O(n) for lookup in each of the first and second methods described.
- Use Cases: Choose the appropriate method based on your specific needs, particularly with regard to the size of the dataset and expected frequency of reverse lookups.
By considering these methods and their advantages or disadvantages, you can select the most appropriate approach depending on your specific needs to retrieve keys from a dictionary using their associated values.
Related reading
- Get keys from HashMap in Java
- Get last element of Stream/List in a one-liner
- Get lengths of a list in a jinja2 template
- Get list from pandas dataframe column or row?
- Get labels from dataset when using tensorflow image_dataset_from_directory
- Get last result in interactive Python shell
- Get list of values for list of keys
- Get modulo from two 4x64bit integer arrays

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.