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
test_dict1 = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 }
test_dict2 = { 'gfg' : 1 , 'is' : 2 , 'good' : 3 }
print ("The original dictionary 1 is : " + str (test_dict1))
print ("The original dictionary 2 is : " + str (test_dict2))
res = {key: test_dict1[key] for key in test_dict1 if key not in test_dict2}
print ("The number of uncommon items are : " + str ( len (res)))
|
Output :
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
test_dict1 = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 }
test_dict2 = { 'gfg' : 1 , 'is' : 2 , 'good' : 3 }
print ("The original dictionary 1 is : " + str (test_dict1))
print ("The original dictionary 2 is : " + str (test_dict2))
res = set (test_dict1.items()) ^ set (test_dict2.items())
print ("The number of uncommon items are : " + str ( len (test_dict1) - len (res)))
|
Output :
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
test_dict1 = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 }
test_dict2 = { 'gfg' : 1 , 'is' : 2 , 'good' : 3 }
keys1 = set (test_dict1.keys())
keys2 = set (test_dict2.keys())
dissimilar_keys = keys1 ^ keys2
freq = sum ([ 1 for key in dissimilar_keys if key in test_dict1])
print ( "The number of uncommon items are : " + str (freq))
|
Output
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
test_dict1 = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 }
test_dict2 = { 'gfg' : 1 , 'is' : 2 , 'good' : 3 }
print ( "The original dictionary 1 is : " + str (test_dict1))
print ( "The original dictionary 2 is : " + str (test_dict2))
keys1 = set (test_dict1.keys())
keys2 = set (test_dict2.keys())
diff_keys = keys1.difference(keys2)
res = len (diff_keys)
print ( "The number of uncommon items are : " + str (res))
|
Output
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).
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
03 May, 2023
Like Article
Save Article