python merge two dictionaries add values

python merge two dictionaries add values

Hi,

In Python I have two dictionaries with same keys. I want to merge the two dictionaries and add the values (int values) into the merged dictionary.

For example student marks in three terms are:

term1 = {"physics":20,"chemistry":20,"math":25,"english":25}
term2 = {"physics":25,"chemistry":28,"math":35,"english":30}
term3 = {"physics":30,"chemistry":25,"math":29,"english":34}

I want to merge and sum the values into one dictionary.

Thanks

View Answers

July 18, 2021 at 5:21 PM

Hi,

For this purpose we can use Counter from Python collections.

What is Python Counter?

Python Counter is a collection library and it is used to hold the count of each of the elements present in the container.

The counter class is a sub-class which is available inside the dictionary class.

Here is the complete example code:

from collections import Counter

term1 = {"physics":20,"chemistry":20,"math":25,"english":25}
term2 = {"physics":25,"chemistry":28,"math":35,"english":30}
term3 = {"physics":30,"chemistry":25,"math":29,"english":34}


sumAllTerms= Counter(term1) + Counter(term2) + Counter(term3)

print(sumAllTerms)

This program generates following output:

>>> from collections import Counter
>>> 
>>> term1 = {"physics":20,"chemistry":20,"math":25,"english":25}
>>> term2 = {"physics":25,"chemistry":28,"math":35,"english":30}
>>> term3 = {"physics":30,"chemistry":25,"math":29,"english":34}
>>> 
>>> 
>>> sumAllTerms= Counter(term1) + Counter(term2) + Counter(term3)
>>> 
>>> print(sumAllTerms)
Counter({'math': 89, 'english': 89, 'physics': 75, 'chemistry': 73})
>>>

Thanks


July 18, 2021 at 5:21 PM

Hi,

Check more tutorials at:

Thanks









Related Tutorials/Questions & Answers:
python merge two dictionaries add values
python merge two dictionaries add values  Hi, In Python I have two dictionaries with same keys. I want to merge the two dictionaries and add...,"math":29,"english":34} I want to merge and sum the values into one dictionary
Merge and sum of two dictionaries
Merge and sum of two dictionaries  Hi, I want to merge the two dictionaries and at the same I want to sum the vales of the same key. How I can... = window.adsbygoogle || []).push({}); Python Tutorials Thanks
Advertisements
Python sum dictionary values by key
values. We are getting these two dictionaries from two different web services...Python sum dictionary values by key  Hi, In python we can easily... these dictionaries into one and sum the dictionary values by key. What
merge two war files
merge two war files  How to merge two war files using maven??? is there any command
merge two war files
merge two war files  how to merge two war files using maven
How to merge two word document using java
How to merge two word document using java   how to merge two word document using java
How to merge two word document using java
How to merge two word document using java   how to merge two word document using java
Applet for add two numbers
Applet for add two numbers  what is the java applet code for add two...); add(text2); label3 = new Label("Sum of Two Numbers...: "); label1.setBounds(20,20,100,20); add(label1); text1 = new TextField(5
ModuleNotFoundError: No module named 'dictionaries'
ModuleNotFoundError: No module named 'dictionaries'  Hi, My Python...;dictionaries' error? Thanks   Hi, In your python environment you have to install padas library. You can install dictionaries python
Add two number in java
Add two number in java In this section you will learn about how to add two... be added, So two add,  convert these String into numeric type..."); System.out.println("Please Enter two number to add :"); Scanner sin=new
two list to dictionary python
two list to dictionary python  Hi, I have two lists and I want to covert it into dictionary. I want to create keys from one list and values from another list. How I can convert two list to dictionary python
Merge Sort Java
. In Merge sort unsorted values are divided into two equal parts, then they are repeatedly merged till the final list is in sorted order completely. Merge Sort...Merge Sort in Java is used to sort integer values of an array. There are many
ModuleNotFoundError: No module named 'nested-dictionaries'
nested-dictionaries python with following command: pip install nested-dictionaries After the installation of nested-dictionaries python library...ModuleNotFoundError: No module named 'nested-dictionaries'  Hi, My
Add two big numbers
Add two big numbers       In this section, you will learn how to add two big numbers...(BigDecimal big): This method is used to add the two BigDecimal numbers. Here
How to add two numbers in Java?
How to add two numbers in a Java program? In Java you can easily add two number by using the plus (+) operator. The plus operator will add two... is: 30 So, in this program you learned how to sum(add) two numbers in Java
Generate Random Integer Between Two Values in Scala
Generate Random Integer Between Two Values in Scala  Hi, I am working on the SCALA project and want to generate random integer between two numbers. How to Generate Random Integer Between Two Values in Scala? Thanks   
Generate Random Integer Between Two Values in Scala
Generate Random Integer Between Two Values in Scala  Hi, I am working on the SCALA project and want to generate random integer between two numbers. How to Generate Random Integer Between Two Values in Scala? Thanks   
Add two big numbers - Java Beginners
Add two big numbers - Java Beginners  Hi, I am beginner in Java and leaned basic concepts of Java. Now I am trying to find example code for adding big numbers in Java. I need basic Java Beginners example. It should easy
How do you combine two dictionary values for common keys
example code in Python. How do you combine two dictionary values for common keys...How do you combine two dictionary values for common keys  Hi, I have two dictionary and I want to combine the values based on the key in new
How to add two calendars on the same html page
How to add two calendars on the same html page  I have used the same code as on this site http://www.roseindia.net/javascript/javascript... use single calendar for a single html page.but while implementing two calendars
Merge Sort In Java
single  element. Merge the two 1/2 values together and sort the values. Do same...; Step2:Merge two values  and sort the values A[0],A[1... to sort integer values of an array using merge sort. In merge sorting
can i insert values into two tables by a query - JDBC
can i insert values into two tables by a query  hi all, can i insert values into two tables by a query Thanks Bala k
how to add data into dictionary in python
({}); Thanks   Hi, Its very easy to add new values in Python dictionary. Here...how to add data into dictionary in python  Hi, I have a python API that returns Python dictionary. It contains many values. Here is simple example
Mysql Merge
Mysql Merge       Mysql Merge Statement is used to merge the two sql statement using UNION clause. The UNION clause is used to combine the result set of any two sql queries
Mysql Merge
Mysql Merge       Mysql Merge Statement is used to merge the two sql statement using UNION clause.The UNION clause is used to combine the result set of any two sql
Java radio buttons and button groups two values
Java radio buttons and button groups two values  hi i have a problem. i have a system to input data from a jform to a mysql database. i have made all of it except the gender radiobuttons. how do i do this? i need when register
Java radio buttons and button groups two values
Java radio buttons and button groups two values  hi i have a problem. i have a system to input data from a jform to a mysql database. i have made all of it except the gender radiobuttons. how do i do this? i need when register
Ajax Multiply Two Values
to multiply two values and display the result on the page. This program calls the method 'callmultiply()' for the multiplying  the values entered by user
how to store array values into two different tables in java?
how to store array values into two different tables in java?  I have... and now I want to store these values in two different tables(i.e store 2 array values in one table and remaining two values in another table
Merge two list into a single list
Description: This example demonstrate how to merge two lists into a single... and the mergelist funtion merge the two lists into a single list. Code: # include... ( struct node *p ) { printf("The data values in the list are\n"
write a program which asks the user for two int values and calculates their sum.
write a program which asks the user for two int values and calculates their sum.  The title says it all, an example is (1,2) > 3
How to store two integer value in a separate file and retrieve those values
How to store two integer value in a separate file and retrieve those values  I need to save two values so i can use those values later i came up with idea of saving those values in a separate file and updating values in file
Add a new item to a dictionary in Python
Add a new item to a dictionary in Python  Hi, I have an dictionary in Python and I want to add new item to it. What is the best way to add an item to a python dictionary? Share me example for adding a new item to a dictionary
python add one day to date
python add one day to date  Hi, I have date object and I want to add one day to the date. How to add one day to the date in Python code? Thanks...) Thanks   Hi, Here are more Python tutorials at: Python Tutorials
python add one day to date
python add one day to date  Hi, I have date object and I want to add one day to the date. How to add one day to the date in Python code? Thanks...) Thanks   Hi, Here are more Python tutorials at: Python Tutorials
python add 1 day to date
python add 1 day to date  Hi, In one of my project I am getting date from the some datasource and now I have to add one day to the date. How I can add 1 day to date in my Python program. Thanks   Hi, Here is example
PHP Array Merge Recursive
/><b>After merging two arrays the values of \$array1 is:</b><br...; string(8) "diligent" } After merging two arrays the values..._dump($array2); echo"<br/><b>After merging two arrays the values
Mysql Merge Statement
Mysql Merge Statement       Mysql Merge Statement is used to merge the two sql statement... two sql queries. Understand with ExampleADS_TO_REPLACE_1 The Tutorial
Mysql Merge Statement
Mysql Merge Statement       Mysql Merge Statement is used to merge the two sql statement using UNION clause. The UNION clause is used to combine the result set of any two
how to add new column before two column in sql.
how to add new column before two column in sql.  how to add new column in table before two column in sql.   Syntax of Alter table: ALTER... named 'employee' having three fields id, name and salary. Now we want to add two
how to add new column before two column in sql.
how to add new column before two column in sql.  how to add new column in table before two column in sql.   Syntax of Alter table: ALTER... named 'employee' having three fields id, name and salary. Now we want to add two
how to add new column before two column in sql.
how to add new column before two column in sql.  how to add new column in table before two column in sql.   Syntax of Alter table: ALTER... named 'employee' having three fields id, name and salary. Now we want to add two
how to add new column before two column in sql.
how to add new column before two column in sql.  how to add new column in table before two column in sql.   Syntax of Alter table: ALTER... named 'employee' having three fields id, name and salary. Now we want to add two
Add Two Numbers in Java
Add Two Numbers in Java     ... into integer type. Now you can add these values into a sum variable and print... is an array of String objects that takes values provided in command prompt
How to make addition of two combobox values using jsp and javascript?
addition of two combobox values using jsp and javascript  How to make addition of two combobox values using jsp and javascript?  <html> <h2>ComboBox</h2> <script language="javascript"> var arr
How can you add elements of two arrayLists in java?
How can you add elements of two arrayLists in java?  if you have an arrayList of tempArr = [hi, my, name, is, john] and another arrayList of tempArray2 = [a, b, c, d, e] and you want it to result in newArr = [hi a, my b, name c
Extra Storage Merge Sort in Java
values divide into two equal parts iteratively and create an array for store data value in extra storage. Then merge the two parts , sort it and store.... Then the following steps are followed by storage merge sort algorithm to sort the values
how to add two object in a particular file - Java Beginners
how to add two object in a particular file  Hi frend.. I have two arraylist object in which there is some data..............now i want to add these two objects in a particular file using file handling in java....and also
Add values of drop down menu( select option) to mysql table
Add values of drop down menu( select option) to mysql table   Here is my code <?php echo $row['valveName'].'<td>'.$row['valveId'].'<td>'; echo '<select name="onoff">'; echo '<
split and merge
split and merge  How to merge two text files into another text file How to split a huge amount of text file into small chunks in Java

Ads