0
0
PythonProgramBeginner · 2 min read

Python Program to Find Common Keys in Two Dictionaries

You can find common keys in two dictionaries using common_keys = dict1.keys() & dict2.keys(), which returns a set of keys present in both dictionaries.
📋

Examples

Inputdict1 = {'a': 1, 'b': 2}, dict2 = {'b': 3, 'c': 4}
Output{'b'}
Inputdict1 = {'x': 10, 'y': 20, 'z': 30}, dict2 = {'w': 5, 'x': 15, 'z': 25}
Output{'x', 'z'}
Inputdict1 = {}, dict2 = {'a': 1}
Outputset()
🧠

How to Think About It

To find common keys, look at the keys of both dictionaries and find which keys appear in both. Since dictionary keys behave like sets, you can use set operations to find the intersection of keys.
📐

Algorithm

1
Get the keys from the first dictionary.
2
Get the keys from the second dictionary.
3
Find the intersection of these two sets of keys.
4
Return or print the common keys.
💻

Code

python
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 4, 'c': 5, 'd': 6}
common_keys = dict1.keys() & dict2.keys()
print(common_keys)
Output
{'b', 'c'}
🔍

Dry Run

Let's trace the example dict1 = {'a': 1, 'b': 2, 'c': 3} and dict2 = {'b': 4, 'c': 5, 'd': 6} through the code

1

Get keys of dict1

dict1.keys() = {'a', 'b', 'c'}

2

Get keys of dict2

dict2.keys() = {'b', 'c', 'd'}

3

Find intersection

{'a', 'b', 'c'} & {'b', 'c', 'd'} = {'b', 'c'}

4

Print result

Output: {'b', 'c'}

Stepdict1.keys()dict2.keys()Intersection
1{'a', 'b', 'c'}
2{'b', 'c', 'd'}
3{'b', 'c'}
💡

Why This Works

Step 1: Keys behave like sets

Dictionary keys can be treated like sets, so you can use set operations like intersection with &.

Step 2: Intersection finds common elements

Using dict1.keys() & dict2.keys() finds keys that exist in both dictionaries.

Step 3: Result is a set of common keys

The result is a set containing all keys found in both dictionaries, which you can use or convert as needed.

🔄

Alternative Approaches

Using set.intersection() method
python
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
common_keys = set(dict1.keys()).intersection(dict2.keys())
print(common_keys)
This is equivalent to using <code>&</code> but uses a method call, which some find clearer.
Using list comprehension
python
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
common_keys = [key for key in dict1 if key in dict2]
print(common_keys)
This returns a list instead of a set and is less efficient but easy to understand for beginners.

Complexity: O(n) time, O(k) space

Time Complexity

Finding keys and intersecting sets takes time proportional to the number of keys in the smaller dictionary, so O(n).

Space Complexity

The space needed is for the set of common keys, which is at most the size of the smaller dictionary's keys, O(k).

Which Approach is Fastest?

Using & or .intersection() is fastest and most readable; list comprehension is slower due to repeated membership checks.

ApproachTimeSpaceBest For
Set intersection (&)O(n)O(k)Fast and clean for large dictionaries
Set.intersection() methodO(n)O(k)Clear method call, same speed as &
List comprehensionO(n*m)O(k)Simple for beginners, slower for large dicts
💡
Use dict1.keys() & dict2.keys() for a clean and fast way to find common keys.
⚠️
Beginners often try to compare dictionaries directly instead of their keys, which does not find common keys.