Get Key from a Dictionary using Value in Python
Is there a key in a dictionary that you want to get? In this article, I will show how you can use the value to get any key from a dictionary using Python.
# create a dicitonary
user = {
"name": "Frank",
"email": "[email protected]",
"phone": 1234567890,
"status": "active"
}
# get all keys from dictionary as a list
user_keys = list(user.keys())
# get all values from dictionary as a list
user_values = list(user.values())
# get index of value item
val_index = user_values.index("[email protected]")
# find key for value
result_key = user_keys[val_index]
# print the result
print(result_key)
Output
email
Dictionaries are useful for storing information you can later use to retrieve a value. In this article, I'll talk about using dictionary values to do something useful. If you're familiar with programming in general, and Python specifically, then you've probably heard of dictionaries. The dictionaries in Python store the data in key-value pair format. We can access the value using a key but what if we want to get the key name using value.
We will explain step by step process to get the key name using value here.
Use keys() and values() functions to get key from value
We can use keys(), values(), and index() functions of python to get the key name from a dictionary using a value.
1. First, create a new dictionary
Create a dictionary first. You can create a dictionary in Python using multiple methods. We are creating a dictionary named company here that contains multiple key-value pair data.
# create a dicitonary
company = {
"name": "Amazon",
"founder": "Jeff Bezos",
"type": "e-commerce",
"location": "New York"
}
2. Get all keys from the dictionary
Now we will create a list that will contain all the keys of the above dictionary. We are using Dictionary.keys() function for that. After getting the keys from the dictionary, we are converting them to Python List using the list() function.
# get all keys from dictionary as a list
company_keys = list(company.keys())
Output
['name', 'founder', 'type', 'location']
3. Get all values from the dictionary
After getting all the key names from the dictionary, we will create a list that will contain the values of the previously declared dictionary. We will be using Dictionary.values() function for that.
# get all values from dictionary as a list
company_values = list(company.values())
Output
['Amazon', 'Jeff Bezos', 'e-commerce', 'New York']
4. Get the index of the value
Now, we will find the index of the value which key name needs to be found. In order to do that, we will use index() function of Python. The index() function return the index value of an item in a List in interger format. It starts from 0. Here, we are finding the index of "e-commerce" value in the company_values list.
# get index of value item
val_index = company_values.index("e-commerce")
Output
2
5. Get the key from value as a result
We can access the items from a list using the item index. Here we have found the index of value "e-commerce" from the company_values list. Now, we will use this index and get the key name from the company_keys list.
# find key for value
result_key = company_keys[val_index]
# print the result
print(result_key)
Output
type
Full Code Example
# create a dicitonary
company = {
"name": "Amazon",
"founder": "Jeff Bezos",
"type": "e-commerce",
"location": "New York"
}
# get all keys from dictionary as a list
company_keys = list(company.keys())
# get all values from dictionary as a list
company_values = list(company.values())
# get index of value item
val_index = company_values.index("e-commerce")
# find key for value
result_key = company_keys[val_index]
# print the result
print(result_key)
Get key from the value of a dictionary using for loop
We cal also use For loop in Python to get the key from the value of a dictionary. You can easily iterate over a dictionary using For Loop and in each iteration, we can check if the value is equal to the value that wants to check.
Code Example
employee = {
"id": 1,
"name": "Mukesh",
"dept": "IT",
"emp_code": "IT001"
}
value = "Mukesh"
for key in employee:
if employee[key] == value:
print(key)
break
Output
name
numbers = {
1: 10,
2: 20,
3: 30,
4: 40,
5: 50
}
num_keys = list(numbers.keys())
num_vals = list(numbers.values())
index = num_vals.index(40)
result = num_keys[index]
print("Key for 40 is: {}".format(result))
# OUTPUT -> Key for 40 is: 4
- Delete a key value pair from a dictionary in Python
- Remove a key from a dictionary Python
- Check if a key exist in a dictionary Python
- Sort a dictionary by value in Python
- Check if a value exists in a dictionary Python
- Get all values by key name from a list of dictionaries Python
- Get dictionary values as a list in python