In this tutorial, we will learn how to add two dictionaries in Python with the same keys. This is a common task when combining data from various sources or aggregating values from two datasets.
Python provides several ways to merge dictionaries, and we will discuss some of the easiest and most commonly used methods.
Step 1: Using a For Loop
A for loop is an easy way to iterate through the keys in two dictionaries and perform the addition. Here’s a step-by-step guide to adding the values of two dictionaries with the same keys using a for loop:
- Create two dictionaries with the same keys.
- Initialize an empty dictionary to store the sum of the values for each key.
- Iterate through the keys of one of the dictionaries.
- For each key, add the values from both dictionaries and store the result in the new dictionary.
Here’s an example:
1 2 3 4 5 6 7 8 |
dict1 = {'a': 1, 'b': 2, 'c': 3} dict2 = {'a': 4, 'b': 5, 'c': 6} result = {} for key in dict1: result[key] = dict1[key] + dict2[key] print(result) |
This will output:
{'a': 5, 'b': 7, 'c': 9}
Step 2: Using Dictionary Comprehension
We can achieve the same result more concisely using dictionary comprehension. A dictionary comprehension is a concise way to create a dictionary in a single line of code.
Here’s how to add two dictionaries using dictionary comprehension:
1 |
result = {key: dict1[key] + dict2[key] for key in dict1} |
This line of code does the same thing as the for loop in the previous step, but it’s shorter and more readable.
Here’s the full code using dictionary comprehension:
1 2 3 4 5 |
dict1 = {'a': 1, 'b': 2, 'c': 3} dict2 = {'a': 4, 'b': 5, 'c': 6} result = {key: dict1[key] + dict2[key] for key in dict1} print(result) |
This will output the same result as before:
{'a': 5, 'b': 7, 'c': 9}
Step 3: Using the update() Method
If you want to update the first dictionary with the sum of both dictionaries, you can use the update() method. The update() method takes a dictionary as an argument and updates the current dictionary with the provided dictionary.
Here’s how to add two dictionaries and update the first dictionary using the update() method:
1 2 |
for key in dict1: dict1.update({key: dict1[key] + dict2[key]}) |
Here’s the full code using the update() method:
1 2 3 4 5 6 7 |
dict1 = {'a': 1, 'b': 2, 'c': 3} dict2 = {'a': 4, 'b': 5, 'c': 6} for key in dict1: dict1.update({key: dict1[key] + dict2[key]}) print(dict1) |
This will output:
{'a': 5, 'b': 7, 'c': 9}
Full Code Examples
Here are the full code examples for each method:
Using a for loop:
1 2 3 4 5 6 7 8 |
dict1 = {'a': 1, 'b': 2, 'c': 3} dict2 = {'a': 4, 'b': 5, 'c': 6} result = {} for key in dict1: result[key] = dict1[key] + dict2[key] print(result) |
Using dictionary comprehension:
1 2 3 4 5 |
dict1 = {'a': 1, 'b': 2, 'c': 3} dict2 = {'a': 4, 'b': 5, 'c': 6} result = {key: dict1[key] + dict2[key] for key in dict1} print(result) |
Using the update() method:
1 2 3 4 5 6 7 |
dict1 = {'a': 1, 'b': 2, 'c': 3} dict2 = {'a': 4, 'b': 5, 'c': 6} for key in dict1: dict1.update({key: dict1[key] + dict2[key]}) print(dict1) |
Conclusion
In this tutorial, we have learned three different methods to add two dictionaries in Python with the same keys. We covered the use of a for loop, dictionary comprehension, and the update() method. Depending on your requirements and coding style, you can choose the method that is most suitable for your specific use case.