Open In App

Check if one dictionary is subset of other – Python

Last Updated : 07 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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)

Output
True

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)

Output
True

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)

Output
True

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)

Output
True

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.



Next Article
Practice Tags :

Similar Reads