In this post, we are going to learn about how to Extract Multiple keys froExtract Multiple keys value from a Python dictionary, often we have one, two, or multiple keys in the dictionary and we have need to extract them based on the condition that we need to extract from the original dictionary and to a new dictionary. In this post, we will understand with examples of how to achieve this by using different techniques.
1. Extract multiple keys values Dictionary Comprehension
In this code example, we are using dictionary comprehension to extract multiple specific keys from the original dictionary. We have a dictionary of three keys{‘math’, ‘chem’, ‘sci’}, and we are extracting the values of these keys, and printing a new dictionary result.
Code Example
#Program to Extract Multiple keys value from a Python dictionary
sub_dict = {'math' : 100, 'chem' : 98, 'sci' : 100,'eng':100}
#key to extract from dictionary
key_to_extract = {'math', 'chem','sci'}
extracted_dict = {key: sub_dict[key] for key in sub_dict.keys()
& key_to_extract}
#filtered keys with valuen result dictionary
print("extracted key-value from dictionary : " , extracted_dict)
Output
Filtered value list is : {'sci': 100, 'math': 100, 'chem': 98}
2. Dictionary Comprehension with IN operator
In this code example, we extract the multiple keys from an original dictionary into the new dictionary. We are using dictionary comprehension with IN operator.
Code Example
#Program to Extract specific keys from a Python dictionary
sub_dict = {'math' : 100, 'chem' : 98, 'sci' : 100,'eng':100}
# keys to extract from dictionary
key_to_extract = {'math', 'chem','sci'}
extracted_dict = { key:value for key,value in sub_dict.items() if key in key_to_extract}
#filtered keys with valuen result dictionary
print("extracted key-value from dictionary : \n" , extracted_dict)
Output
extracted key-value from dictionary :
{'math': 100, 'chem': 98, 'sci': 100}
3. Dict() with list comprehension to extract multiple keys values
In this code example, we are using list comprehension to extract multiple specific key-value pairs into the new dictionary and later using dict() constructor to convert them into the dictionary.
Code Example
#Program to Extract specific keys from a Python dictionary
sub_dict = {'math' : 100, 'chem' : 98, 'sci' : 100,'eng':100}
# #key to extract from dictionary
key_to_extract = {'math', 'chem','sci'}
extracted_dict = dict((key,sub_dict[key] ) for key in key_to_extract
if key in sub_dict)
#filtered keys with valuen result dictionary
print("extracted key-value from dictionary : \n" , extracted_dict)
Output
extracted key-value from dictionary :
{'math': 100, 'sci': 100, 'chem': 98}
4. Dictionary Comprehension to extract multiple keys values based on condition
In this code example, we are extracting multiples key from the dictionary using dictionary comprehension and condition using if statement by checking if values of key is equal to 100(which is our criteria condition).
In the output result, it is showing how many keys satisfy the given condition.
#Program to Extract specific keys from a Python dictionary
sub_dict = {'math' : 100, 'chem' : 98, 'sci' : 100,'eng':100}
extracted_dict = { key:value for key, value in sub_dict.items() if value ==100 }
#filtered keys with values result dictionary
print("extracted key-value from dictionary : \n" , extracted_dict)
Output
extracted key-value from dictionary :
{'math': 100, 'sci': 100, 'eng': 100}
5. Nested ‘for’ loop to Extract multiple keys values Python dictionary
In this code example, we are doing extraction of specific keys from a Python dictionary by using a nested for loop. The outer loop runs for the original dictionary(sub_dict) keys, and the inner loop runs for the keys dictionary(key_to_extract). We are looking at the dictionary, If the key match in both dictionaries then we are adding into a new dictionary (extracted_dict).
#Program to Extract specific keys from a Python dictionary
sub_dict = {'math' : 100, 'chem' : 98, 'sci' : 100,'eng':100}
extracted_dict = {}
# keys to extract
key_to_extract = {'math', 'chem','sci'}
for keyoriginal in sub_dict:
for key in key_to_extract:
if keyoriginal == key:
extracted_dict[key] = sub_dict[key]
#filtered keys with valuen result dictionary
print("extracted key-value from dictionary : \n" , extracted_dict)
Output
extracted key-value from dictionary :
{'math': 100, 'chem': 98, 'sci': 100}
Conclusion
We have learned different ways to extract Multiple keys values from a Python dictionary. Now by using these ways we can easily extract specific keys from the original dictionary to the new dictionary and use them in data manipulation.
Happy Learning!!