Open In App

Get a Subset of Dict in Python

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

In Python, dictionaries store key-value pairs and are pivotal in data management. Extracting dictionary subsets based on specific criteria is key for targeted data analysis, ensuring operational efficiency by focusing on relevant data. This technique, vital in data processing and machine learning, allows programmers to handle data selectively, optimizing performance and relevance. Mastering subset extraction is therefore crucial for efficient data manipulation and precise information retrieval in Python programming. In this article, we will see how we can get a subset of a dictionary in Python.

Get A Subset Of Dict in Python

Below are some of the ways by which we can get a subset of the dictionary in Python:

  • Using Dictionary Comprehension
  • Using filter() and lambda
  • Using map() and dict.get() method
  • Using itemgetter() method

Get A Subset Of Dict Using Dictionary Comprehension

In this example, a subset dictionary is created from the original_dict, including only the key-value pairs where the keys are present in the keys_to_include set using dictionary comprehension. The resulting subset_dict will contain {‘a’: 1, ‘d’: 4}.

Python3




original_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
keys_to_include = {'a', 'd'}
 
subset_dict = {k: original_dict[k] for k in keys_to_include}
print(subset_dict)


Output

{'a': 1, 'd': 4}


Get A Subset Of Dict Using filter() and lambda

In this example, a subset dictionary is generated by using filter(), lambda and filtering key-value pairs from the original_dict based on the keys present in the keys_to_include set, resulting in the subset_dict {‘a’: 1, ‘c’: 3}.

Python3




original_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
keys_to_include = {'a', 'c'}
 
subset_dict = dict(filter(lambda item: item[0] in keys_to_include, original_dict.items()))
print(subset_dict)


Output

{'a': 1, 'c': 3}


Get A Subset Of Dict Using map() and dict.get()

In this example, a subset dictionary is created by zipping the keys_to_include list with the corresponding values from original_dict using the map function, resulting in the subset_dict {‘a’: 1, ‘c’: 3}.

Python3




original_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
keys_to_include = ['a', 'c']
 
subset_dict = dict(zip(keys_to_include, map(original_dict.get, keys_to_include)))
print(subset_dict)


Output

{'a': 1, 'c': 3}


Get A Subset Of Dict Using itemgetter() Method

In this example, using the itemgetter() from the operator module, values corresponding to the keys in keys_to_include are obtained from the original_dict. The script then ensures that the values are treated as a tuple if only one key is included and combines the keys with their corresponding values to create the subset_dict, resulting in {‘a’: 1, ‘c’: 3}.

Python3




from operator import itemgetter
original_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
keys_to_include = ['a', 'c']
 
# Get values corresponding to the keys in keys_to_include
values = itemgetter(*keys_to_include)(original_dict)
 
# If only one key to include, make it a tuple
if len(keys_to_include) == 1:
    values = (values,)
 
# Combine keys and values
subset_dict = dict(zip(keys_to_include, values))
print(subset_dict)


Output

{'a': 1, 'c': 3}




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads