Python – Frequency of unequal items in Dictionary

Sometimes, while working with Python, we can come across a problem in which we need to check for the unequal items count among two dictionaries. This has an application in cases of web development and other domains as well. Let’s discuss certain ways in which this task can be performed.
Method #1: Using dictionary comprehension This particular task can be performed in one line using dictionary comprehension which offers a way of compacting lengthy brute logic and just checks for unequal items and increments count.
Python3
# Python3 code to demonstrate working of# Dissimilar items frequency in Dictionary# Using dictionary comprehension# initializing dictionariestest_dict1 = {'gfg' : 1, 'is' : 2, 'best' : 3}test_dict2 = {'gfg' : 1, 'is' : 2, 'good' : 3}# printing original dictionariesprint("The original dictionary 1 is : " + str(test_dict1))print("The original dictionary 2 is : " + str(test_dict2))# Dissimilar items frequency in Dictionary# Using dictionary comprehensionres = {key: test_dict1[key] for key in test_dict1 if key not in test_dict2}# printing resultprint("The number of uncommon items are : " + str(len(res))) |
The original dictionary 1 is : {'best': 3, 'is': 2, 'gfg': 1}
The original dictionary 2 is : {'good': 3, 'is': 2, 'gfg': 1}
The number of uncommon items are : 1
Method #2: Using set() + XOR operator + items() The combination of above methods can be used to perform this particular task. In this, the set function removes duplicates and XOR operator computes the matched items. Result can be computed by subtracting original dict. length by new dict. length.
Python3
# Python3 code to demonstrate working of# Dissimilar items frequency in Dictionary# Using set() + XOR operator + items()# initializing dictionariestest_dict1 = {'gfg' : 1, 'is' : 2, 'best' : 3}test_dict2 = {'gfg' : 1, 'is' : 2, 'good' : 3}# printing original dictionariesprint("The original dictionary 1 is : " + str(test_dict1))print("The original dictionary 2 is : " + str(test_dict2))# Dissimilar items frequency in Dictionary# Using set() + XOR operator + items()res = set(test_dict1.items()) ^ set(test_dict2.items())# printing resultprint("The number of uncommon items are : " + str(len(test_dict1) - len(res))) |
The original dictionary 1 is : {'best': 3, 'is': 2, 'gfg': 1}
The original dictionary 2 is : {'good': 3, 'is': 2, 'gfg': 1}
The number of uncommon items are : 1
Time Complexity: O(n), where n is the number of elements in the larger of the two dictionaries being compared.
Auxiliary Space: O(min(n1, n2)), where n1 and n2 are the number of elements in the two dictionaries being compared.
Method #3: Using set() + XOR operator + items()
- Initialize the dictionaries test_dict1 and test_dict2.
- Convert the keys of the dictionaries into sets using the set() function and store them in keys1 and keys2.
- Find the dissimilar keys between the two dictionaries using the XOR operator ^ and store them in dissimilar_keys.
- Use a list comprehension to count the number of dissimilar keys present in test_dict1 and store it in freq.
- Print the value of freq to get the number of uncommon items.
Python3
# initializing dictionariestest_dict1 = {'gfg': 1, 'is': 2, 'best': 3}test_dict2 = {'gfg': 1, 'is': 2, 'good': 3}# getting the keys of the dictionarieskeys1 = set(test_dict1.keys())keys2 = set(test_dict2.keys())# finding the dissimilar keys using XOR operatordissimilar_keys = keys1 ^ keys2# getting the frequency of the dissimilar keysfreq = sum([1 for key in dissimilar_keys if key in test_dict1])# printing resultprint("The number of uncommon items are : " + str(freq)) |
The number of uncommon items are : 1
Time complexity: O(n), where n is the total number of items in both dictionaries. This is because we are iterating over the keys of the dictionaries once to find the dissimilar keys and once again to count their frequency.
Auxiliary space: O(n), where n is the total number of items in both dictionaries. This is because we are storing the keys of both dictionaries in memory.
Method #4: Using set() + difference() method
- Convert the keys of both dictionaries into sets using set() method.
- Find the difference of these sets using difference() method which will give us the dissimilar items.
- Find the length of the resulting set to get the number of dissimilar items.
Python3
# Python3 code to demonstrate working of# Dissimilar items frequency in Dictionary# Using set() + difference() method# initializing dictionariestest_dict1 = {'gfg' : 1, 'is' : 2, 'best' : 3}test_dict2 = {'gfg' : 1, 'is' : 2, 'good' : 3}# printing original dictionariesprint("The original dictionary 1 is : " + str(test_dict1))print("The original dictionary 2 is : " + str(test_dict2))# Dissimilar items frequency in Dictionary# Using set() + difference() methodkeys1 = set(test_dict1.keys()) # convert keys of dict1 to setkeys2 = set(test_dict2.keys()) # convert keys of dict2 to setdiff_keys = keys1.difference(keys2) # find the difference of two setsres = len(diff_keys)# printing resultprint("The number of uncommon items are : " + str(res)) |
The original dictionary 1 is : {'gfg': 1, 'is': 2, 'best': 3}
The original dictionary 2 is : {'gfg': 1, 'is': 2, 'good': 3}
The number of uncommon items are : 1
Time Complexity: O(n) where n is the number of keys in both dictionaries, as set() method takes O(n) time complexity in worst case, and difference() method also takes O(n) time complexity in worst case. Thus, overall time complexity is O(n).
Auxiliary Space: O(n), as we are creating two sets of keys which would take extra space of O(n).



