 GeeksforGeeks App
Open App Browser
Continue

# Python | Get key from value in Dictionary

Let’s see how to get the key by value in Python Dictionary.

Example: One-Liner code

## Python3

 `# creating a new dictionary``my_dict ``=``{``"Java"``:``100``, ``"Python"``:``112``, ``"C"``:``11``}` `# one-liner``print``(``"One line Code Key value: "``, ``list``(my_dict.keys())``      ``[``list``(my_dict.values()).index(``100``)])`

Output:

`Java`

Extract Key from Python Dictionary using Value

### Method 1: Get the key by value using list comprehension

A list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element in the Python list to get the key from a value in Dictionary.

## Python3

 `dic ``=``{``"geeks"``: ``"A"``,``"for"``:``"B"``,``"geeks"``:``"C"``}` `value ``=` `{i ``for` `i ``in` `dic ``if` `dic[i]``=``=``"B"``}``print``(``"key by value:"``,value)`

Output:

`key by value: {'for'}`

Time Complexity: O(N)
Auxiliary Space: O(1)

### Method 2: Get the key by value using a list.index()

The index() method returns the index of the corresponding value in a list. 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 a corresponding value at position N in val_list.

## Python3

 `# 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 with val 100``position ``=` `val_list.index(``100``)``print``(key_list[position])`

Output:

`java`

Time complexity: O(1)
Auxiliary space: O(1)

### Method 3: Get the key by value using dict.item()

We can also fetch the key from a value by matching all the values using the dict.item() and then printing the corresponding key to the given value.

## Python3

 `# 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``))`

Output

```Java
C
```

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(1), as the space used by the function does not depend on the size of the input dictionary.

### Method 4: Using lambda and filter()

Here is an example of using the filter() function to get the key corresponding to a value in a dictionary:

## Python3

 `my_dict ``=` `{``"Java"``: ``100``, ``"Python"``: ``112``, ``"C"``: ``11``}` `# Get the key corresponding to value 100``key ``=` `list``(``filter``(``lambda` `x: my_dict[x] ``=``=` `100``, my_dict))[``0``]``print``(key)``#This code is contributed by Edula Vinay Kumar Reddy`

Output

`Java`

In this example, the filter() function is used to create a list of keys from my_dict where the value is equal to 100. The resulting list is then indexed at position 0 to get the first element, which is the key corresponding to the value 100.

Time complexity: O(n), as the filter() function needs to iterate through the entire dictionary to create the list of keys.
Auxiliary space is O(n), as the list of keys created by filter() has a size equal to the number of elements in the dictionary.

METHOD 5:Using items method

This code finds the key of a given value in a dictionary by using a list comprehension to iterate over the items in the dictionary and check if the value matches the given value. If a key is found, it is added to a list, and the first element of the list is printed as the key for the given value. If the value is not found in the dictionary, a message is printed indicating that it was not found.

Steps:

1. Use the items method of the dictionary to loop through each key-value pair in my_dict.
2. Check if the value associated with the current key is equal to the given value.
3. If it is equal, append the current key to a list of keys.
4. If the loop completes without finding a matching value, print a message indicating that the value was not found.
5. If a matching key was found, print the first key in the list.

## Python3

 `# input list``my_dict ``=` `{``"Java"``: ``100``, ``"Python"``: ``112``, ``"C"``: ``11``}``value ``=` `112` `key_list ``=` `[key ``for` `key, val ``in` `my_dict.items() ``if` `val ``=``=` `value]` `if` `len``(key_list) > ``0``:``    ``print``(``"The key for the value"``, value, ``"is"``, key_list[``0``])``else``:``    ``print``(``"Value not found in dictionary"``)`

Output

`The key for the value 112 is Python`

Time complexity: O(N), where n is the number of key-value pairs in the dictionary.
Auxiliary Space: O(K), where k is the number of keys that match the given value.

METHOD 6:Using re module

The program uses the re module in Python to find the key in a dictionary that corresponds to a given value. It creates a regular expression pattern that matches the value surrounded by word boundaries, then uses the next() function to iterate over the items in the dictionary and search for a value that matches the pattern. If a match is found, the program returns the corresponding key.

ALGORITHM:

1. Create a regular expression pattern that matches the value we’re looking for, surrounded by word boundaries.
2. Use the next() function to iterate over the items in the dictionary, searching for a value that matches the pattern.
3. If a match is found, return the corresponding key. Otherwise, return None.

## Python3

 `import` `re` `# Given input``my_dict ``=` `{``"Java"``: ``100``, ``"Python"``: ``112``, ``"C"``: ``11``}``value ``=` `100` `# Program code``pattern ``=` `re.``compile``(r``'\b'` `+` `str``(value) ``+` `r``'\b'``)``key ``=` `next``((k ``for` `k, v ``in` `my_dict.items() ``if` `pattern.search(``str``(v))), ``None``)``print``(key)`

Output

`Java`

Time complexity: O(N), Creating the regular expression pattern takes O(1) time. Searching for a value that matches the pattern in each dictionary item takes O(n) time in the worst case, where n is the number of items in the dictionary. The next() function and the if statement each takes O(1) time. Therefore, the overall time complexity of the program is O(n).

Auxiliary Space: O(1), The regular expression pattern, and the key variable each require O(1) space. The pattern variable and the generator expression inside the next() function both require O(1) space. The k and v variables used in the generator expression do not require additional space, as they are created as part of the iteration. Therefore, the overall space complexity of the program is O(1).

My Personal Notes arrow_drop_up