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
Find the Common Keys from two Dictionaries in Python
Dictionaries are key-value data structures in Python where each key is unique. Finding common keys between two dictionaries is a frequent task in data processing and comparison operations.
Let's explore different methods to find common keys with this example ?
Input:
dict1 = {'I': 10, 'II': 20, 'III': 30, 'IV': 40}
dict2 = {'II': 40, 'V': 60, 'VI': 80, 'I': 90}
Expected Output: {'I', 'II'}
Using Set Intersection with keys()
The most efficient approach uses set intersection to find common keys ?
dict1 = {'A': 20, 'T': 30, 'W': 40, 'S': 100, 'E': 80}
dict2 = {'E': 200, 'B': 450, 'S': 100, 'A': 20}
common_keys = dict1.keys() & dict2.keys()
print("Common keys:", common_keys)
print("As a list:", list(common_keys))
Common keys: {'A', 'S', 'E'}
As a list: ['A', 'S', 'E']
Using intersection() Method
The intersection() method provides explicit set operations ?
dict1 = {"ab": 11, "bc": 21, "cd": 13, "de": 41}
dict2 = {"cd": 33, "ad": 14, "de": 51, "fa": 16}
common_keys = set(dict1.keys()).intersection(set(dict2.keys()))
print("Common keys:", sorted(common_keys))
# Alternative syntax
common_keys2 = set(dict1).intersection(dict2)
print("Alternative way:", sorted(common_keys2))
Common keys: ['cd', 'de'] Alternative way: ['cd', 'de']
Using List Comprehension
For more control over the output format, use list comprehension ?
dict1 = {"a": 1, "b": 2, "c": 3, "d": 4}
dict2 = {"b": 2, "c": 5, "e": 6, "f": 7}
common_keys = [key for key in dict1 if key in dict2]
print("Common keys (ordered):", common_keys)
# With values from both dictionaries
common_with_values = [(key, dict1[key], dict2[key]) for key in dict1 if key in dict2]
print("Keys with values:", common_with_values)
Common keys (ordered): ['b', 'c']
Keys with values: [('b', 2, 2), ('c', 3, 5)]
Finding Common Key-Value Pairs
To find keys with identical values in both dictionaries ?
dict1 = {"a": 1, "b": 2, "c": 3, "d": 4}
dict2 = {"b": 2, "c": 3, "e": 5, "f": 6}
# Common keys only
common_keys = dict1.keys() & dict2.keys()
print("Common keys:", common_keys)
# Common key-value pairs
common_items = dict1.items() & dict2.items()
print("Common key-value pairs:", dict(common_items))
Common keys: {'b', 'c'}
Common key-value pairs: {'b': 2, 'c': 3}
Comparison of Methods
| Method | Performance | Return Type | Best For |
|---|---|---|---|
keys() & |
Fastest | Set | Simple key comparison |
intersection() |
Fast | Set | Explicit set operations |
| List comprehension | Medium | List | Ordered results or filtering |
items() & |
Medium | Set of tuples | Matching key-value pairs |
Conclusion
Use dict1.keys() & dict2.keys() for the fastest common key detection. For matching key-value pairs, use dict1.items() & dict2.items(). List comprehension offers more control for complex filtering requirements.
