Python Dictionaries allow you to store values in key-value pairs.
You can get the key by value in a dictionary using the dict.items()
and an if
statement in Python.
This tutorial teaches you how to get the key by value in a dictionary in Python using different methods.
Creating a Dictionary
First, you’ll create a dictionary that will be used to learn how to get keys by value.
The sample dictionary contains five key-value pairs. And it has a value 4 for two keys.
Code
yourdict = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"fourr": 4
}
list(yourdict)
To add keys after creating a dictionary, you can read How to Add Keys to Dictionary
Output
When you print the dictionary, you’ll see the below output.
{'one': 1, 'two': 2, 'three': 3, 'four': 4, 'fourr': 4}
Using Dict.Items()
This section teaches you how to get key by value in a dictionary using the dict.items() method.
When you iterate through a dictionary using the dict.items()
method, you’ll get both the key
and value
of the current iteration.
Check if the current iteration’s value equals the value
you desire to get the key
of.
- If it is equal, then you can use the
key
. - If it is not equal, you can ignore the current iteration.
Code
The below code demonstrates how to get the key
of the value 1 by iterating the dictionary using the dict.items()
method.
value_to_get_key = 1
for key, val in yourdict.items():
if val == value_to_get_key:
print(key)
Output
You’ll see the key of the value 1 printed.
one
This is how you can get the key by value in a dictionary using the dict.items()
method.
Using List Comprehension
List comprehension provides a simpler syntax to iterate over an iterable and create a new list based on the values in the iterable.
- It provides you with a list of values that pass the specified condition.
Here also, you need to use the dict.items()
method to iterate over the dictionary and comprehend a new list based on a value.
Use this method when you want to identify a list of keys
with the same value.
Code
The code below demonstrates how to use list comprehension to get the list of keys
based on a value
.
value_to_get_key = 1
keys = [key for key,val in yourdict.items() if val == value_to_get_key]
keys
Output
['one']
This is how you can get the key using a value in a dictionary in Python.
Get All Keys With A Specific Value
This section teaches you how to get all keys with a specific value in a python dictionary.
- iterate over the dictionary using the
dict.items()
and list comprehension. - Check if the value is equal to the value you desire.
- If yes, you’ll return the key to the list comprehension, creating a new list of
keys
.
The resultant list will contain the keys that have a specific value.
Code
The following code demonstrates how to get all keys with a specific value.
value_to_get_key = 4
keys = [key for key,val in yourdict.items() if val == value_to_get_key]
keys
Output
The sample dictionary contains two keys with a value of 4. Hence those keys will be available in the list.
['four', 'fourr']
This is how you can get all the keys with a specific value.
Get Key Of Value With a Specific Condition
To get key of value with a specific condition.
- Use the
dict.items()
to iterate over the dictionary - Check if the value passes the specific condition using the
if
statement.
Code
The code below demonstrates how to get the key of value with a specific condition using dict.items()
and an if
statement.
value_to_get_key = 2
keys = [key for key,val in yourdict.items() if val > value_to_get_key]
keys
Output
['three', 'four', 'fourr']