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...,"chemistry":25,"math":29,"english":34} I want to merge and sum the values
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... at: Python Tutorials Thanks
Advertisements
Python sum dictionary values by key
values. We are getting these two dictionaries from two different web services. We want to merge these dictionaries into one and sum the dictionary values by key...Python sum dictionary values by key  Hi, In python we can easily
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
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
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? Thanks   
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
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 '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
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 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
How do you combine two dictionary values for common keys
? I want example code in Python. How do you combine two dictionary values...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
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
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
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
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
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
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
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
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
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"
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... this will print: Sum is: 30 So, in this program you learned how to sum(add) two numbers
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 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
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
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
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 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
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
how to get values for same column name from two different tables in SQL
how to get values for same column name from two different tables in SQL  how to get values for same column name from two different tables in SQL???? column name is emp_id loacated in these two tables company,employee
how to add data into dictionary in python
in python? Thanks   Hi, Its very easy to add new values in Python...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
Merge of two arrays in C
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
Java Merge Array
important regarding arrays i.e merging of arrays - add two or more arrays... to enter their values and call this method. This method merge all the values of arrays...Java Merge Array You all are aware of Arrays, their structure, declaration
ModuleNotFoundError: No module named 'merge'
ModuleNotFoundError: No module named 'merge'  Hi, My Python... padas library. You can install merge python with following command: pip install merge After the installation of merge python library
How to compare two tables, and insert values which r not in one table to another table?
How to compare two tables, and insert values which r not in one table to another table?  Hi I need to compare two tables MainTable and OUTTable... insert the values to main_table... here is my sql query insert into Main_Table
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

Ads