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.
Dictionaries in Python are a fundamental data structure that store data in key-value pairs, enabling quick access to values by their keys. However, there are situations where you might need to retrieve a key using a value, a task that is not as straightforward as the inverse operation. In this article, we will explore how to get a key by value in a Python dictionary and discuss several methods and considerations to handle this operation efficiently.
Understanding the Basics of Python Dictionaries
A dictionary in Python is defined using curly braces {}, with key-value pairs separated by commas. Each key is linked to its corresponding value by a colon :. Keys in dictionaries are unique and are typically strings or numbers, while values can be of any data type and can repeat.
Here's a simple dictionary for demonstration:
Retrieving Key by Value
By default, dictionaries are designed to retrieve value by key, not the other way around. Therefore, if you need to find a key based on a value, you will have to iterate over the dictionary. Here is the basic method to do that:
This function get_key_by_value takes a dictionary and a value as arguments, iterates through dictionary items, and returns the key when the matching value is found.
Handling Multiple Keys With the Same Value
A value may appear in multiple key-value pairs. If you need to retrieve all keys for a particular value, you can modify the above function to return a list of keys.
Performance Considerations
Finding a key by value involves a linear search, which has a time complexity of , where is the number of elements in the dictionary. This is because this operation scans each element until a match is found or all elements have been checked.
Summary Table
| Method | Use-case | Complexity | Return Type | Consideration |
get_key_by_value() | Single key retrieval | Single key or None | Only one key will be returned, even if multiple keys have the same value | |
get_all_keys_by_value() | Multiple keys retrieval | List of keys | Useful when values are not unique and multiple keys need retrieval |
Advanced Usage and Tips
- Looking for Similar Values: For non-exact matches or complex data structures, additional logic might be necessary, like substring matches or threshold-based matches.
- Using Inverted Dictionaries: If value-to-key retrieval is common in your application, consider maintaining an inverted dictionary where values are keys. This requires extra space but lookup becomes .
- Deep Dictionaries: For nested dictionaries, recursive techniques or specialized functions like
deep_get()might be used to access keys deeply nested within the dictionary structure.
Conclusion
Retrieving keys by value in a dictionary requires iterating over the dictionary, making it less efficient than key-to-value retrieval. Always consider the characteristics of your data and use cases when choosing how to implement this functionality, and weigh the trade-offs between time complexity and space complexity.
Related reading
- Get key by value in dictionary
- 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 labels from dataset when using tensorflow image_dataset_from_directory
- Get last result in interactive Python shell
- Get list from pandas dataframe column or row?
- Get list of values for list of keys

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.