- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding the Common items among Python dictionaries
A Dictionary is one of the unordered data structures available in python to store the data in the key and value pair. It is also known as Associative array or Hash map in other programming languages. The dictionary is represented using the curly braces {} and the key and value are separated using a colon “:” The keys in the dictionary are unique and the values can be of duplicates. To access the elements of the dictionary we will use the keys.
Example
The following is the example of creating a dictionary using the dic() method and curly braces {} available in python.
# creating dictionary using dict() method: l = [('name', 'John'), ('age', 25), ('city', 'New York')] dic = dict(l) print("The dictionary created using dict() method",dic) # creating dictionary using {}: dic = {'name' : 'John', 'age' : 25, 'city' :'New York'} print("The dictionary created using {}",dic)
Output
The dictionary created using dict() method {'name': 'John', 'age': 25, 'city': 'New York'} The dictionary created using {} {'name': 'John', 'age': 25, 'city': 'New York'}
There are multiple approaches for finding the common items among python dictionaries. Let’s see each approach in detail.
Using Set Intersection
One straightforward approach is to convert the keys of the dictionaries into sets and then use the set intersection operation & to find the common keys.
Example
In this example, we first convert the keys of dict1 and dict2 into sets using set(dict1.keys()) and set(dict2.keys()). Then, set intersection & operator performs the set intersection, resulting in a set of common keys. Finally, we create a new dictionary, common_items, by iterating over the common keys and fetching the corresponding values from dict1.
dict1 = {'a': 1, 'b': 2, 'c': 3} dict2 = {'b': 20, 'c': 30, 'd': 40} common_keys = set(dict1.keys()) & set(dict2.keys()) common_items = {key: dict1[key] for key in common_keys} print("The common items in the dictionaries dict1,dict2:",common_items)
Output
The common items in the dictionaries dict1,dict2: {'c': 3, 'b': 2}
Using Dictionary Comprehension
The approach is to use a dictionary comprehension which directly creates a new dictionary with only the common key-value pairs.
Example
In this approach, we iterate over the keys of dict1 using for key in dict1, and for each key, we check if it exists in dict2 using the condition if key in dict2. If the condition is true, we include the key-value pair in the common_items dictionary.
dict1 = {'a': 1, 'b': 2, 'c': 3} dict2 = {'b': 20, 'c': 30, 'd': 40} common_items = {key: dict1[key] for key in dict1 if key in dict2} print("The common items in the dictionaries dict1,dict2:",common_items)
Output
The common items in the dictionaries dict1,dict2: {'b': 2, 'c': 3}
Using the items() Method
The items() method returns a view object that contains key-value pairs of the dictionary. We can use this method to iterate over the items and filter out the common items.
Example
In this approach, we use the items() method to iterate over the key-value pairs of dict1 using for key, value in dict1.items(). We then check if the key exists in dict2 and if the corresponding values in both dictionaries are the same. If these conditions are met, we include the key-value pair in the common_items dictionary.
dict1 = {'a': 1, 'b': 2, 'c': 3} dict2 = {'b': 20, 'c': 30, 'd': 40} common_items = {key: value for key, value in dict1.items() if key in dict2 and dict2[key] == value} print("The common items in the dictionaries dict1,dict2:",common_items)
Output
The common items in the dictionaries dict1,dict2: {}