Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
Creating a Dictionary
The following is the example of creating a dictionary using the dict() 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)
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 ?
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 20, 'c': 30, 'd': 40}
# Find common keys using set intersection
common_keys = set(dict1.keys()) & set(dict2.keys())
print("Common keys:", common_keys)
# Create dictionary with common keys and values from dict1
common_items = {key: dict1[key] for key in common_keys}
print("Common items (with values from dict1):", common_items)
Common keys: {'b', 'c'}
Common items (with values from dict1): {'b': 2, 'c': 3}
Using Dictionary Comprehension
The approach is to use a dictionary comprehension which directly creates a new dictionary with only the common key-value pairs ?
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 20, 'c': 30, 'd': 40}
# Find common keys and their values from dict1
common_items = {key: dict1[key] for key in dict1 if key in dict2}
print("Common items (with values from dict1):", common_items)
Common items (with values from dict1): {'b': 2, 'c': 3}
Finding Common Keys with Same Values
The items() method returns a view object that contains key-value pairs of the dictionary. We can use this method to find items where both key and value match ?
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 2, 'c': 30, 'd': 40} # Changed b to 2 for demonstration
# Find items where both key and value are identical
common_items = {key: value for key, value in dict1.items() if key in dict2 and dict2[key] == value}
print("Items with same key and value:", common_items)
Items with same key and value: {'b': 2}
Working with Multiple Dictionaries
You can extend these approaches to find common items among more than two dictionaries ?
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 20, 'c': 30, 'd': 40}
dict3 = {'b': 5, 'c': 3, 'e': 50}
# Find common keys among all three dictionaries
common_keys = set(dict1.keys()) & set(dict2.keys()) & set(dict3.keys())
print("Common keys in all three dictionaries:", common_keys)
# Create result dictionary with values from first dictionary
result = {key: dict1[key] for key in common_keys}
print("Common items:", result)
Common keys in all three dictionaries: {'b', 'c'}
Common items: {'b': 2, 'c': 3}
Comparison
| Method | Best For | Performance |
|---|---|---|
| Set Intersection | Multiple dictionaries | Fast for large dictionaries |
| Dictionary Comprehension | Simple cases with filtering | Good readability |
| items() Method | Matching both keys and values | Slower for large dictionaries |
Conclusion
Use set intersection for finding common keys efficiently across multiple dictionaries. Use dictionary comprehension for readable code when working with two dictionaries. Use the items() method when you need to match both keys and values exactly.
