Python | Common items among dictionaries
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 code to demonstrate the working of # Equal items among dictionaries # Using dictionary comprehension # initializing dictionaries test_dict1 = {'gfg' : 1, 'is' : 2, 'best' : 3} test_dict2 = {'gfg' : 1, 'is' : 2, 'good' : 3} # printing original dictionaries print("The original dictionary 1 is : " + str(test_dict1)) print("The original dictionary 2 is : " + str(test_dict2)) # Equal items among dictionaries # Using dictionary comprehension res = {key: test_dict1[key] for key in test_dict1 if key in test_dict2 and test_dict1[key] == test_dict2[key]} # printing result print("The number of common items are : " + str(len(res))) |
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 code to demonstrate working of # Equal items among dictionaries # Using set() + XOR operator + items() # initializing dictionaries test_dict1 = {'gfg' : 1, 'is' : 2, 'best' : 3} test_dict2 = {'gfg' : 1, 'is' : 2, 'good' : 3} # printing original dictionaries print("The original dictionary 1 is : " + str(test_dict1)) print("The original dictionary 2 is : " + str(test_dict2)) # Equal items among dictionaries # Using set() + XOR operator + items() res = set(test_dict1.items()) ^ set(test_dict2.items()) # printing result print("The number of common items are : " + str(len(res))) |
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
Recommended Posts:
- Get() method for dictionaries in Python
- Python | Merging two Dictionaries
- Python | Sum list of dictionaries with same key
- Python | Subtraction of dictionaries
- Python | Flatten given list of dictionaries
- Python | Merging two list of dictionaries
- Python | Set Difference in list of dictionaries
- Python | Get values of particular key in list of dictionaries
- Python | Intersect two dictionaries through keys
- Python | Difference in keys of two dictionaries
- Python | Segregating key's value in list of dictionaries
- Python | Combine the values of two dictionaries having same key
- Python | Sort given list of dictionaries by date
- Python | Removing dictionary from list of dictionaries
- Python | Initialize list with empty dictionaries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


