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
Python - Difference in keys of two dictionaries
Two Python dictionaries may contain some common keys between them. In this article we will find how to get the difference in the keys present in two given dictionaries.
Using Set Difference
Here we take two dictionaries and apply set function to them. Then we subtract the two sets to get the difference. We do it both ways, by subtracting second dictionary from first and next subtracting first dictionary from second. Those keys which are not common are listed in the result set ?
Example
dictA = {'1': 'Mon', '2': 'Tue', '3': 'Wed'}
print("1st Dictionary:\n", dictA)
dictB = {'3': 'Wed', '4': 'Thu', '5': 'Fri'}
print("2nd Dictionary:\n", dictB)
res1 = set(dictA) - set(dictB)
res2 = set(dictB) - set(dictA)
print("\nKeys in dictA but not in dictB:", res1)
print("Keys in dictB but not in dictA:", res2)
Running the above code gives us the following result −
1st Dictionary:
{'1': 'Mon', '2': 'Tue', '3': 'Wed'}
2nd Dictionary:
{'3': 'Wed', '4': 'Thu', '5': 'Fri'}
Keys in dictA but not in dictB: {'1', '2'}
Keys in dictB but not in dictA: {'4', '5'}
Using For Loop with 'in' Operator
In another approach we can use a for loop to iterate through the keys of one dictionary and check for its presence using the in clause in the second dictionary ?
Example
dictA = {'1': 'Mon', '2': 'Tue', '3': 'Wed'}
print("1st Dictionary:\n", dictA)
dictB = {'3': 'Wed', '4': 'Thu', '5': 'Fri'}
print("2nd Dictionary:\n", dictB)
print("\nThe keys in 1st dictionary but not in the second:")
for key in dictA.keys():
if key not in dictB:
print(key)
print("\nThe keys in 2nd dictionary but not in the first:")
for key in dictB.keys():
if key not in dictA:
print(key)
Running the above code gives us the following result −
1st Dictionary:
{'1': 'Mon', '2': 'Tue', '3': 'Wed'}
2nd Dictionary:
{'3': 'Wed', '4': 'Thu', '5': 'Fri'}
The keys in 1st dictionary but not in the second:
1
2
The keys in 2nd dictionary but not in the first:
4
5
Using Keys Difference Method
Dictionary keys support set operations. We can directly use the difference method on dictionary keys to find the different keys ?
Example
dictA = {'1': 'Mon', '2': 'Tue', '3': 'Wed'}
dictB = {'3': 'Wed', '4': 'Thu', '5': 'Fri'}
# Keys in dictA but not in dictB
diff1 = dictA.keys() - dictB.keys()
print("Keys in dictA but not in dictB:", diff1)
# Keys in dictB but not in dictA
diff2 = dictB.keys() - dictA.keys()
print("Keys in dictB but not in dictA:", diff2)
# All different keys (symmetric difference)
all_diff = dictA.keys() ^ dictB.keys()
print("All different keys:", all_diff)
Running the above code gives us the following result −
Keys in dictA but not in dictB: {'1', '2'}
Keys in dictB but not in dictA: {'4', '5'}
All different keys: {'1', '2', '4', '5'}
Comparison
| Method | Performance | Best For |
|---|---|---|
| Set difference | Fast | Large dictionaries |
| For loop with 'in' | Slower | Small dictionaries or custom logic |
| Keys difference | Fast | Most readable and Pythonic |
Conclusion
Use the keys difference method (dict.keys() - dict.keys()) for the most Pythonic approach. Set operations are efficient for finding different keys between dictionaries.
