Python | Subtraction of dictionaries
Sometimes, while working with dictionaries, we might have utility problem in which we need to perform elementary operation among the common keys of dictionaries. This can be extended to any operation to be performed. Let’s discuss subtraction of like key values and ways to solve it in this article.
Method #1 : Using dictionary comprehension + keys()
The combination of above two can be used to perform this particular task. This is just a shorthand to the longer method of loops and can be used to perform this task in one line.
# Python3 code to demonstrate working of # Subtraction of dictionaries # Using dictionary comprehension + keys() # 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()} # printing result print("The difference dictionary is : " + str(res)) |
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}
Method #2 : Using Counter() + "-" operator
The combination of above method can be used to perform this particular task. In this, the Counter function converts the dictionary in the form in which the minus operator can perform the task of subtraction.
# Python3 code to demonstrate working of # Subtraction of dictionaries # Using Counter() + "-" operator 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 : " + str(test_dict1)) print("The original dictionary 2 : " + str(test_dict2)) # Using Counter() + "-" operator # Subtraction of dictionaries temp1 = Counter(test_dict1) temp2 = Counter(test_dict2) res = temp2 - temp1 # printing result print("The difference dictionary is : " + str(dict(res))) |
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}
Recommended Posts:
- Python | Merging two Dictionaries
- Get() method for dictionaries in Python
- Python | Sum list of dictionaries with same key
- Python | Segregating key's value in list of dictionaries
- Python | Set Difference in list of dictionaries
- Python | Combine the values of two dictionaries having same key
- Python | Intersect two dictionaries through keys
- Python | Common items among dictionaries
- Python | Difference in keys of two dictionaries
- Python | Merging two list of dictionaries
- Python | Flatten given list of dictionaries
- Python | Get values of particular key in list of dictionaries
- Handling missing keys in Python dictionaries
- Python | Removing dictionary from list of dictionaries
- Python | Sort given list of dictionaries by date
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



