Python | Common items among dictionaries
Last Updated :
16 May, 2023
Sometimes, while working with Python, we can come across a problem in which we need to check for the equal 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 equal 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 in test_dict2 and test_dict1[key] = = test_dict2[key]}
print ("The number of common items are : " + str ( len (res)))
|
Output :
The original dictionary 1 is : {'gfg': 1, 'best': 3, 'is': 2}
The original dictionary 2 is : {'gfg': 1, 'is': 2, 'good': 3}
The number of common items are : 2
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.
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 common items are : " + str ( len (res)))
|
Output :
The original dictionary 1 is : {'gfg': 1, 'best': 3, 'is': 2}
The original dictionary 2 is : {'gfg': 1, 'is': 2, 'good': 3}
The number of common items are : 2
Method #3: Using the & operator
Another approach to finding the common items among dictionaries is to use the & operator. The & operator compares the keys of the two dictionaries and returns the keys that are present in both dictionaries.
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))
common_keys = set (test_dict1.keys()) & set (test_dict2.keys())
print ( "The common items are : " + str ( len (common_keys)))
|
Output
The original dictionary 1 is : {'gfg': 1, 'is': 2, 'best': 3}
The original dictionary 2 is : {'gfg': 1, 'is': 2, 'good': 3}
The common items are : 2
Time complexity: O(n)
Auxiliary Space: O(n), where n is number of items in both dictionaries.
Method 4 : use a loop and compare the keys of both dictionaries.
Two dictionaries, test_dict1 and test_dict2, are initialized with some key-value pairs.
The original dictionaries are printed using the print() function and string concatenation.
An empty list, common_keys, is created to store the common keys between the two dictionaries.
A for loop is used to iterate through the keys of test_dict1.
For each key in test_dict1, the if statement checks if it also exists in test_dict2.
If the key exists in test_dict2, it is added to the common_keys list using the append() method.
After all the keys of test_dict1 have been checked, the len() function is used to find the number of common keys, which is then printed using the print() function and string concatenation.
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))
common_keys = []
for key in test_dict1:
if key in test_dict2:
common_keys.append(key)
print ( "The common items are : " + str ( len (common_keys)))
|
Output
The original dictionary 1 is : {'gfg': 1, 'is': 2, 'best': 3}
The original dictionary 2 is : {'gfg': 1, 'is': 2, 'good': 3}
The common items are : 2
The time complexity of this method is O(n), where n is the number of keys in test_dict1. The auxiliary space required is O(k), where k is the number of common keys between the dictionaries.
METHOD 5:Using counter method.
APPROACH:
This program finds the number of common items between two dictionaries using the Counter method of the collections module in Python. It converts the input dictionaries into Counter objects and calculates the intersection of the two counters to find the common items. Finally, it returns the count of common items.
ALGORITHM:
1. Define the input dictionaries.
2. Create Counter objects from the dictionaries using the Counter() function of the collections module.
3. Calculate the intersection of the two counters using the & operator.
4. Convert the resulting counter back into a dictionary.
5. Get the count of common items in the resulting dictionary.
6. Print the result.
Python3
import collections
dict1 = { 'gfg' : 1 , 'best' : 3 , 'is' : 2 }
dict2 = { 'gfg' : 1 , 'is' : 2 , 'good' : 3 }
counter1 = collections.Counter(dict1)
counter2 = collections.Counter(dict2)
common_items = dict (counter1 & counter2)
print ( "The original dictionary 1 is:" , dict1)
print ( "The original dictionary 2 is:" , dict2)
print ( "The number of common items are:" , len (common_items))
|
Output
The original dictionary 1 is: {'gfg': 1, 'best': 3, 'is': 2}
The original dictionary 2 is: {'gfg': 1, 'is': 2, 'good': 3}
The number of common items are: 2
Time Complexity:
The time complexity of this program is O(n), where n is the total number of items in both dictionaries. This is because the Counter() function has a time complexity of O(n), and finding the intersection of the counters also takes O(n) time.
Space Complexity:
The space complexity of this program is O(m), where m is the total number of distinct items in both dictionaries. This is because the Counter() function creates a dictionary-like object that stores the count of each item, and the resulting dictionary only stores the common items. Therefore, the space used by the program is proportional to the number of distinct items, rather than the total number of items.
Please Login to comment...