Get specific keys' values - Python
Last Updated :
12 Jul, 2025
Our task is to retrieve values associated with specific keys from a dictionary. This is especially useful when we only need to access certain pieces of data rather than the entire dictionary. For example, suppose we have the following dictionary: d = {'name': 'John', 'age': 25, 'location': 'New York', 'job': 'Engineer'} if we need to get the values for the keys 'name' and 'job' then the output should be: {'name': 'John', 'job': 'Engineer'}
Using dictionary comprehension
We can use dictionary comprehension to achieve a more compact version of the loop method.
Python
d = {'name': 'John', 'age': 25, 'location': 'New York', 'job': 'Engineer'}
filter_key = ['name', 'job']
res = {key: d[key] for key in filter_key if key in d}
print(res)
Output{'name': 'John', 'job': 'Engineer'}
Explanation:
- In this approach, dictionary comprehension is used to create the result dictionary by checking if each key in filter_key exists in d.
- If the key is found then we add it along with its value to the result dictionary.
Using a loop
We can iterate through the dictionary and check if the key exists then retrieve its value. This is a simple approach to get specific values based on the keys.
Python
d = {'name': 'John', 'age': 25, 'location': 'New York', 'job': 'Engineer'}
filter_key = ['name', 'job']
res = {}
for key in filter_key:
if key in d:
res[key] = d[key]
print(res)
Output{'name': 'John', 'job': 'Engineer'}
Explanation: We define a list filter_key that contains the keys whose values we want to retrieve and then loop through each key in this list, if the key is present in the dictionary then we add the key-value pair to the result dictionary.
Using filter()
filter() function can also be used to filter out keys that we want to retrieve from the dictionary. It’s more functional in style and allows us to process the keys more flexibly.
Python
d = {'name': 'John', 'age': 25, 'location': 'New York', 'job': 'Engineer'}
filter_key = ['name', 'job']
res = dict(filter(lambda item: item[0] in filter_key, d.items()))
print(res)
Output{'name': 'John', 'job': 'Engineer'}
Explanation:
- d.items() gives us the key-value pairs of the dictionary and filter() function is used with a lambda function to check if the key is in the filter_key list.
- The result of filter() is converted back into a dictionary using dict().
Using map()
map() function can be used to apply a function to each item in an iterable allowing us to process the keys in the dictionary. We can then filter out the values for the specified keys.
Python
d = {'name': 'John', 'age': 25, 'location': 'New York', 'job': 'Engineer'}
filter_key = ['name', 'job']
res = dict(map(lambda key: (key, d[key]) if key in d else (key, None), filter_key))
# Remove any keys that are not found in the dictionary
res = {key: val for key, val in res.items() if val is not None}
print(res)
Output{'name': 'John', 'job': 'Engineer'}
Explanation:
- map() is used to apply a lambda function to each key in filter_key and if the key exists in d, the key-value pair is included in the result otherwise it returns None.
- After applying map() we filter out the None values using a dictionary comprehension to get the final result.
Similar Reads
Python | Search Key from Value The problem of finding a value from a given key is quite common. But we may have a problem in which we wish to get the back key from the input key we feed. Let's discuss certain ways in which this problem can be solved. Method #1 : Using Naive Method In this method, we just run a loop for each of th
4 min read
Python Update Dictionary Value by Key A Dictionary in Python is an unordered collection of key-value pairs. Each key must be unique, and you can use various data types for both keys and values. Dictionaries are enclosed in curly braces {}, and the key-value pairs are separated by colons. Python dictionaries are mutable, meaning you can
3 min read
Get Dictionary Value by Key - Python We are given a dictionary and our task is to retrieve the value associated with a given key. However, if the key is not present in the dictionary we need to handle this gracefully to avoid errors. For example, consider the dictionary : d = {'name': 'Alice', 'age': 25, 'city': 'New York'} if we try t
3 min read
Python Dict Get Value by Key Default We are given a dictionary in Python where we store key-value pairs and our task is to retrieve the value associated with a particular key. Sometimes the key may not be present in the dictionary and in such cases we want to return a default value instead of getting an error. For example, if we have a
4 min read
Python Iterate Dictionary Key, Value In Python, a Dictionary is a data structure that stores the data in the form of key-value pairs. It is a mutable (which means once created we modify or update its value later on) and unordered data structure in Python. There is a thing to keep in mind while creating a dictionary every key in the dic
3 min read
Iterate Through Specific Keys in a Dictionary in Python Sometimes we need to iterate through only specific keys in a dictionary rather than going through all of them. We can use various methods to iterate through specific keys in a dictionary in Python.Using dict.get() MethodWhen we're not sure whether a key exists in the dictionary and don't want to rai
3 min read