Open In App

Extract Subset of Key-Value Pairs from Python Dictionary

Last Updated : 19 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will study different approaches by which we can Extract the Subset Of Key-Value Pairs From the Python Dictionary. When we work with Python dictionaries, it often involves extracting subsets of key-value pairs based on specific criteria. This can be useful for tasks such as filtering, transforming, or manipulating data.

Extract Subset Of Key-Value Pairs From Dictionary in Python

Below are some of the ways by which we can extract a subset of key-value pairs from the dictionary in Python:

  • Using Dictionary Comprehension
  • Using filter() Function
  • Using dict() Constructor and Items()
  • Using Keys() Method with Dictionary Comprehension
  • Using itemgetter() from the operator Module

Extract Subset Of Key-Value Pairs Using Dictionary Comprehension

In this approach, we use dictionary comprehension to create a new dictionary by iterating through the original dictionary. It includes only those key-value pairs where the key is in the specified set {‘a’, ‘b’}.

Python3




# initializing dictionary
original_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
 
# Creating a new dictionary with only key-value pairs where the key is in {'a', 'b'}
subset_dict = {key: value for key, value in original_dict.items() if key in {
    'a', 'b'}}
# Printing dictionary
print("Subset dictionary using Dictionary Comprehension:", subset_dict)


Output

Subset dictionary using Dictionary Comprehension: {'a': 1, 'b': 2}


Extract Subset Of Key-Value Pairs Using filter() Function

In this approach, we use the filter() function to create an iterable of key-value pairs based on a condition. Here, it checks if the key is in the set {‘a’, ‘b’}. The result is then converted back to a dictionary.

Python3




original_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
 
# Using filter() to get an iterable of key-value pairs based on the condition
subset_items = filter(lambda item: item[0] in {
                      'a', 'b'}, original_dict.items())
 
# Converting the iterable back to a dictionary
subset_dict = dict(subset_items)
print("Subset dictionary using filter() Function:", subset_dict)


Output

Subset dictionary using filter() Function: {'a': 1, 'b': 2}


Extract Subset Of Key-Value Pairs Using dict() Constructor and Items()

In this approach, we create a dictionary by iterating through the specified keys and adding key-value pairs to the new dictionary.

Python3




# initializing dictionary
original_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
 
# Creating a dictionary directly using the dict() constructor and a comprehension
subset_dict = dict((key, original_dict[key])
                   for key in {'a', 'b'} if key in original_dict)
 
# Printing dictionary
print("Subset dictionary using dict() Constructor and Items():", subset_dict)


Output

Subset dictionary using dict() Constructor and Items(): {'b': 2, 'a': 1}


Extract Subset Of Key-Value Pairs Using Keys() Method

In this approach, we use the keys() method to get the keys from the original dictionary and after that we use the dictionary comprehension which creates a new dictionary with key-value pairs where the key is in the specified set {‘a’, ‘b’}.

Python3




# initializing dictionary
original_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
keys_to_extract = {'a', 'b'}
 
# Creating a new dictionary with only key-value pairs where the key is in keys_to_extract
subset_dict = {key: original_dict[key]
               for key in original_dict.keys() & keys_to_extract}
 
# Printing dictionary
print("Subset dictionary using Keys() Method with Dictionary Comprehension:", subset_dict)


Output

Subset dictionary using Keys() Method with Dictionary Comprehension: {'a': 1, 'b': 2}


Extract Subset Of Key-Value Pairs Using itemgetter()

In this approach, we use the itemgetter() function from the operator module to extract values based on specified keys. Also zip() function combines keys and values, and in turn a new dictionary is created.

Python3




from operator import itemgetter
 
# initializing dictionary
original_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
keys_to_extract = {'a', 'b'}
 
subset_dict = dict(
    zip(keys_to_extract, itemgetter(*keys_to_extract)(original_dict)))
 
# Printing dictionary
print("Subset dictionary using itemgetter() from the operator Module:", subset_dict)


Output

Subset dictionary using itemgetter() from the operator Module: {'a': 1, 'b': 2}


Conclusion

We have covered easy methods on “How To Extract Subset Of Key-Value Pairs From Python Dictionary”, we can easily extract a subset of key-value pairs from a dictionary using these methods. Accessing values in a dictionary is very important for data manipulation, updation, deletion, etc. 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads