VOOZH about

URL: https://www.geeksforgeeks.org/python/python-get-key-from-value-in-dictionary/

⇱ Get Key from Value in Dictionary - Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Get Key from Value in Dictionary - Python

Last Updated : 11 Jul, 2025

The goal is to find the keys that correspond to a particular value. Since dictionaries quickly retrieve values based on keys, there isn't a direct way to look up a key from a value.

Using next() with a Generator Expression

This is the most efficient when we only need the first matching key. This method uses a generator expression inside the next() function to efficiently retrieve the first matching key for the given value. The generator stops once it finds the first match, making it fast for one-time lookups.

Example:


Output
['b', 'c']

Explanation:

  • d.items(): Iterates over all key-value pairs in the dictionary.
  • Generator Expression: (key for key, val in d.items() if val == tar) yields the first key where the value matches tar.
  • next(): Retrieves the first matching key. If no match is found, it returns None.

Let's explore other methods to get key from value in Dictionary:

Using Dictionary Comprehension

Dictionary comprehension provides a concise way to find all keys that correspond to the given value. It processes the entire dictionary and returns a list of all matching keys.

Example:


Output
['b', 'c']

Explanation:

  • [key for key, val in d.items() if val == tar] iterates through the dictionary, collecting all keys where the value matches val.
  • keys: Stores the list of matching keys.

Using filter()

filter() function can be used to filter out keys where their value matches the target. The result is an iterable that can be converted to a list.

Example:


Output
['b', 'c']

Explanation:

  • filter() takes a lambda function and an iterable. Lambda checks if the value associated with each key matches value_to_find.
  • list() converts the filtered iterable into a list of keys.

Using defaultdict (for multiple keys with the same value)

This method uses defaultdict to group keys by their corresponding values. It is useful when we need to handle cases where multiple keys share the same value.


Output
['b', 'c']

Explanation:

  • defaultdict(list): Creates a dictionary where each value is a list that will hold all keys corresponding to that value.
  • for key, val in d.items():: Iterates through the dictionary.
  • res[val].append(key): Appends each key to the list of the value in the defaultdict.

Inverting Dictionary (for Reverse Lookup)

Inverting dictionary swaps keys and values so that we can perform reverse lookups. However, if multiple keys have the same value, only the last key will be retained.

Example:

Explanation:

  • {v: k for k, v in d1.items()}: Creates a new dictionary where the keys and values are swapped. This allows for quick reverse lookups.
  • d2.get(val): Looks up the original key for the given value. Since only one key can correspond to a value, this method works best when values are unique.

Using a for loop

This method iterates over the dictionary using a for loop, checking each key-value pair. When a match is found, it immediately returns the key and exits the loop.

Example:


Output
b

Explanation:

  • d.items(): Iterates through each key-value pair.
  • if val == tar:: Checks if the value matches the target.
  • print(key): Prints the first matching key and exits the loop with break to avoid further unnecessary checks.
Comment