Python | Subtraction of dictionaries
Last Updated :
12 Jul, 2025
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
# Initialize dictionaries
test_dict1 = {'gfg' : 6, 'is' : 4, 'best' : 7}
test_dict2 = {'gfg' : 10, 'is' : 6, 'best' : 10}
# printing original dictionaries
print("The original dictionary 1 : " + str(test_dict1))
print("The original dictionary 2 : " + str(test_dict2))
# Using dictionary comprehension + keys()
# Subtraction of dictionaries
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
# Initialize dictionaries
test_dict1 = {'gfg': 6, 'is': 4, 'best': 7}
test_dict2 = {'gfg': 10, 'is': 6, 'best': 10}
# printing original dictionaries
print("The original dictionary 1:", test_dict1)
print("The original dictionary 2:", test_dict2)
# Using Counter() + "-" operator
# Subtraction of dictionaries
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}
# printing original dictionaries
print("The original dictionary 1 : ", test_dict1)
print("The original dictionary 2 : ", test_dict2)
# Using set() and dictionary comprehension
# Subtraction of dictionaries
res = {key: test_dict2[key] - test_dict1[key] for key in set(test_dict1) & set(test_dict2)}
print("The difference dictionary is : ", res)
OutputThe 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 = {}
# printing original dictionaries
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)
OutputThe 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}
# Initialize an empty dictionary to store the difference
diff_dict = {}
# Iterate over the key-value pairs of both dictionaries
for key, value in test_dict1.items():
if key in test_dict2:
# If the key is present in both dictionaries, compute the difference in values
diff_dict[key] = test_dict2[key] - value
else:
# If the key is not present in test_dict2, set the difference value to the value in test_dict1
diff_dict[key] = -value
# Iterate over the key-value pairs of test_dict2
for key, value in test_dict2.items():
if key not in test_dict1:
# If the key is not present in test_dict1, set the difference value to the value in test_dict2
diff_dict[key] = value
# Print the original dictionaries and the difference dictionary
print("The original dictionary 1 : ", test_dict1)
print("The original dictionary 2 : ", test_dict2)
print("The difference dictionary is : ", diff_dict)
OutputThe 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.
Similar Reads
Python - Symmetric Difference of Dictionaries Given two Dictionaries, the task is to write a Python program to get the symmetric difference. Examples: Input : test_dict1 = {'Gfg' : 4, 'is' : 3, 'best' : 7, 'for' : 3, 'geek' : 4}, test_dict2 = {'Gfg' : 4, 'is' : 3, 'good' : 7, 'for' : 3, 'all' : 4} Output : {'all': 4, 'good': 7, 'best': 7, 'geek
6 min read
How to Compare Two Dictionaries in Python In this article, we will discuss how to compare two dictionaries in Python. The simplest way to compare two dictionaries for equality is by using the == operator.Using == operatorThis operator checks if both dictionaries have the same keys and values.Pythond1 = {'a': 1, 'b': 2} d2 = {'a': 1, 'b': 2}
2 min read
Python Remove Dictionary Item Sometimes, we may need to remove a specific item from a dictionary to update its structure. For example, consider the dictionary d = {'x': 100, 'y': 200, 'z': 300}. If we want to remove the item associated with the key 'y', several methods can help achieve this. Letâs explore these methods.Using pop
2 min read
Python | Nested Tuples Subtraction Sometimes, while working with records, we can have a problem in which we require to perform index-wise subtraction of tuple elements. This can get complicated with tuple elements being tuples and inner elements again being tuple. Letâs discuss certain ways in which this problem can be solved. Metho
7 min read
Python | How to get Subtraction of tuples Sometimes, while working with records, we might have a common problem of subtracting the contents of one tuple with the corresponding index of other tuple. This has application in almost all the domains in which we work with tuple records. Letâs discuss certain ways in which this task can be perform
5 min read
Subtract Two Numbers in Python Subtracting two numbers in Python is a basic operation where we take two numeric values and subtract one from the other. For example, given the numbers a = 10 and b = 5, the result of a - b would be 5. Let's explore different methods to do this efficiently.Using Minus Operator (-)This is the most st
2 min read