Check if one dictionary is subset of other - Python
Last Updated :
07 Apr, 2025
Checking if one dictionary is a subset of another involves verifying whether all key-value pairs of the smaller dictionary exist in the larger dictionary with the same values. For example, given two dictionaries a = {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5} and b = {'gfg': 1, 'is': 2, 'best': 3}, we need to determine if all entries in b are present in a. Since every key-value pair in b matches a corresponding pair in a, b is a subset of a. Let's explore different methods to achieve this.
Using set()
This method uses Python’s set operations to determine if one dictionary is a subset of another. By converting both dictionaries’ items (key-value pairs) to sets, we can efficiently check if all items from dictionary b exist in dictionary a using the issubset() method. Set operations are optimized in Python for speed.
Python
a = {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
b = {'gfg': 1, 'is': 2, 'best': 3}
res = set(b.items()).issubset(a.items())
print(res)
Explanation: This code checks if all key-value pairs in b exist in a by converting both dictionaries to sets of items and using issubset(). Since b's pairs are fully contained in a, the result is True.
Using dictionary comprehension
In this approach, dictionary comprehension is used to filter the key-value pairs from dictionary b that are also present in dictionary a. The resulting dictionary’s length is compared to b's length to confirm that all items from b exist in a. While this method is slightly less efficient than set operations.
Python
a = {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
b = {'gfg': 1, 'is': 2, 'best': 3}
res = len({k: v for k, v in b.items() if a.get(k) == v}) == len(b)
print(res)
Explanation: This code filters key-value pairs from b that match in a using dictionary comprehension. It checks if the length of the filtered dictionary equals b's length. If all key-value pairs in b exist in a with the same values, the result is True otherwise False.
Using all()
all() combined with the get() to check if every key-value pair from dictionary b exists in dictionary a. It iterates over each item in b and verifies if the corresponding key in a holds the same value. This approach is easy to understand and works well for small to medium-sized dictionaries.
Python
a = {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
b = {'gfg': 1, 'is': 2, 'best': 3}
res = all(a.get(key, None) == val for key, val in b.items())
print(res)
Explanation: This code checks whether all key-value pairs in b exist in a and have the same values using all(). It iterates over b.items(), comparing each value with a.get(key, None). Since all comparisons are True, the result is True.
Using loop
In this method, a manual for loop iterates through each key-value pair in b and checks if it matches the corresponding key-value pair in a. If any pair doesn’t match, the loop terminates early. While this is a simple and straightforward approach, it is less efficient than the previous methods
Python
a = {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
b = {'gfg': 1, 'is': 2, 'best': 3}
res = True
for key, val in b.items():
if a.get(key) != val:
res = False
break
print(res)
Explanation: This code iterates over b.items(), checking if each key-value pair exists in a with the same value. If a mismatch is found, res is set to False and the loop breaks early.
Similar Reads
Python | Check if key has Non-None value in dictionary Sometimes, while working with Python dictionaries, we might come across a problem in which we need to find if a particular key of the dictionary is valid i.e it is not False or has a non-none value. This kind of problem can occur in the Machine Learning domain. Let's discuss certain ways in which th
6 min read
Check if Value Exists in Python Dictionary We are given a dictionary and our task is to check whether a specific value exists in it or not. For example, if we have d = {'a': 1, 'b': 2, 'c': 3} and we want to check if the value 2 exists, the output should be True. Let's explore different ways to perform this check in Python.Naive ApproachThis
2 min read
Python - Nested Dictionary Subset Given a Nested Dictionary, test if another dictionary is a subset. Examples: Input : test_dict = {"gfg": 12, 'best' : {1 : 3, 4 : 3, 'geeks' : {8 : 7}}}, sub_dict = {8 : 7} Output : True Explanation : Required Nested dictionary present in Dictionary.Input : test_dict = {"gfg": 12, 'best' : {1 : 3, 4
7 min read
Python | Check for None values in given dictionary Many times, while working with dictionaries, we wish to check for a non-null dictionary, i.e check for None values in given dictionary. This finds application in Machine Learning in which we have to feed data with no none values. Let's discuss certain ways in which this task can be performed. Method
7 min read
Python - Check for Key in Dictionary Value list Sometimes, while working with data, we might have a problem we receive a dictionary whose whole key has list of dictionaries as value. In this scenario, we might need to find if a particular key exists in that. Let's discuss certain ways in which this task can be performed. Method #1: Using any() Th
6 min read