Python | Subtraction of dictionaries
Last Updated :
17 Aug, 2023
Sometimes, while working with dictionaries, we might have a utility problem in which we need to perform elementary operations among the common keys of dictionaries in Python. This can be extended to any operation to be performed. Let’s discuss the subtraction of like key values and ways to solve it in this article.
Example
Input: The original dictionary 1 : {'gfg': 6, 'is': 4, 'best': 7}
The original dictionary 2 : {'gfg': 10, 'is': 6, 'best': 10}
Output: The difference dictionary is : {'gfg': 4, 'is': 2, 'best': 3}
Explanantion: In this, we are subtacting 2 dictionary and printing the difference dictionary using Python.
How to Subtract Two Dictionaries
In Python, we have different methods to subtract dictionaries. Here we will see different methods:
- Using dictionary comprehension
- Using Counter() + “-” operator
- Using a for loop and dict.get()
- Using items() method
Subtract Values from two Dictionaries using dictionary comprehension + keys()
The combination of the above two can be used to perform this particular task. This is just a shorthand for the longer method of loops and can be used to perform this task in one line.
Python3
test_dict1 = { 'gfg' : 6 , 'is' : 4 , 'best' : 7 }
test_dict2 = { 'gfg' : 10 , 'is' : 6 , 'best' : 10 }
print ( "The original dictionary 1 : " + str (test_dict1))
print ( "The original dictionary 2 : " + str (test_dict2))
res = {key: test_dict2[key] - test_dict1.get(key, 0 )
for key in test_dict2.keys()}
print ( "The difference dictionary is : " + str (res))
|
Output:
The original dictionary 1 : {'gfg': 6, 'is': 4, 'best': 7}
The original dictionary 2 : {'gfg': 10, 'is': 6, 'best': 10}
The difference dictionary is : {'gfg': 4, 'is': 2, 'best': 3}
Subtracting values between two dictionaries using Counter() + “-” operator
The combination of the above methods can be used to perform this particular task. In this, the Counter function converts the dictionary into the form in which the minus operator can perform the task of subtraction.
Python3
from collections import Counter
test_dict1 = { 'gfg' : 6 , 'is' : 4 , 'best' : 7 }
test_dict2 = { 'gfg' : 10 , 'is' : 6 , 'best' : 10 }
print ( "The original dictionary 1:" , test_dict1)
print ( "The original dictionary 2:" , test_dict2)
temp1 = Counter(test_dict1)
temp2 = Counter(test_dict2)
res = temp2 - temp1
print ( "The difference dictionary is:" , dict (res))
|
Output
The original dictionary 1: {'gfg': 6, 'is': 4, 'best': 7}
The original dictionary 2: {'gfg': 10, 'is': 6, 'best': 10}
The difference dictionary is: {'gfg': 4, 'is': 2, 'best': 3}
Dictionary Subtraction using the built-in function set() and dictionary comprehension
This method uses the built-in function set() to iterate through the keys of both dictionaries. The keys that are common in both dictionaries are subtracted and the result is stored in a new dictionary.
Python3
test_dict1 = { 'gfg' : 6 , 'is' : 4 , 'best' : 7 }
test_dict2 = { 'gfg' : 10 , 'is' : 6 , 'best' : 10 }
print ( "The original dictionary 1 : " , test_dict1)
print ( "The original dictionary 2 : " , test_dict2)
res = {key: test_dict2[key] - test_dict1[key] for key in set (test_dict1) & set (test_dict2)}
print ( "The difference dictionary is : " , res)
|
Output
The original dictionary 1 : {'gfg': 6, 'is': 4, 'best': 7}
The original dictionary 2 : {'gfg': 10, 'is': 6, 'best': 10}
The difference dictionary is : {'best': 3, 'is': 2, 'gfg': 4}
Time complexity: O(n)
Auxiliary space: O(n) where n is the number of common keys in both dictionaries.
Subtract Values from two Dictionaries using a for loop and dict.get()
By using a for
loop along with the dict.get()
method. This method helps iterate through the keys of one dictionary and, using, dict.get()
check if the same key exists in the other dictionary. If it does, the corresponding key-value pair can be removed.
Python3
test_dict1 = { 'gfg' : 6 , 'is' : 4 , 'best' : 7 }
test_dict2 = { 'gfg' : 10 , 'is' : 6 , 'best' : 10 }
diff_dict = {}
print ( "The original dictionary 1 : " , test_dict1)
print ( "The original dictionary 2 : " , test_dict2)
for key in test_dict2.keys():
diff_dict[key] = test_dict2[key] - test_dict1.get(key, 0 )
print ( "The difference dictionary is : " , diff_dict)
|
Output
The original dictionary 1 : {'gfg': 6, 'is': 4, 'best': 7}
The original dictionary 2 : {'gfg': 10, 'is': 6, 'best': 10}
The difference dictionary is : {'gfg': 4, 'is': 2, 'best': 3}
Time Complexity: O(N)
Auxiliary Space: O(N)
Subtracting values between two dictionaries using items() Method
We can use the items() method to iterate over the key-value pairs of both dictionaries and check for differences in values.
Initialize the two dictionaries test_dict1 and test_dict2. Initialize an empty dictionary diff_dict to store the difference between the two dictionaries. Iterate over the key-value pairs of test_dict1 using the items() method. For each key:
- If the key is present in test_dict2, compute the difference in values and add it to diff_dict.
- If the key is not present in test_dict2, set the difference value to the negative of the value in test_dict1.
Iterate over the key-value pairs of test_dict2 using the items() method. For each key:
- If the key is not present in test_dict1, set the difference value to the value in test_dict2.
Print the original dictionaries and the different dictionaries.
Python3
test_dict1 = { 'gfg' : 6 , 'is' : 4 , 'best' : 7 }
test_dict2 = { 'gfg' : 10 , 'is' : 6 , 'best' : 10 }
diff_dict = {}
for key, value in test_dict1.items():
if key in test_dict2:
diff_dict[key] = test_dict2[key] - value
else :
diff_dict[key] = - value
for key, value in test_dict2.items():
if key not in test_dict1:
diff_dict[key] = value
print ( "The original dictionary 1 : " , test_dict1)
print ( "The original dictionary 2 : " , test_dict2)
print ( "The difference dictionary is : " , diff_dict)
|
Output
The original dictionary 1 : {'gfg': 6, 'is': 4, 'best': 7}
The original dictionary 2 : {'gfg': 10, 'is': 6, 'best': 10}
The difference dictionary is : {'gfg': 4, 'is': 2, 'best': 3}
Time Complexity: O(n), where n is the size of the larger dictionary.
Auxiliary Space: O(n), where n is the size of the larger dictionary.
Share your thoughts in the comments
Please Login to comment...