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 dictionary.

For example if we have following two dictionary:

d1 = {"a":200, "b":200, "c":500}

d2 = {"a":300, "b":300, "c":800}

The final dictionary should be::

final = {"a":500, "b":500, "c":1300}

How to combine values in a new dictionary?

I want example code in Python. How do you combine two dictionary values for common keys?

Share me running code.

Thanks

View Answers

July 18, 2021 at 5:09 PM

Hi,

Here is the final program that sums values from two dictionary:

from collections import Counter

d1 = {"a":200, "b":200, "c":500}

d2 = {"a":300, "b":300, "c":800}

combinedDict = Counter(d1) + Counter(d2)
print(combinedDict)

Above example program shows you how to combine two dictionary values for common keys.

If you run the program you will get following output:

>>> 
>>> from collections import Counter
>>> 
>>> d1 = {"a":200, "b":200, "c":500}
>>> 
>>> d2 = {"a":300, "b":300, "c":800}
>>> 
>>> combinedDict = Counter(d1) + Counter(d2)
>>> print(combinedDict)
Counter({'c': 1300, 'a': 500, 'b': 500})
>>>

Thanks


July 18, 2021 at 5:10 PM

Hi,

Check more tutorials at:

Thanks

Ads









Related Tutorials/Questions & Answers:
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...? I want example code in Python. How do you combine two dictionary values
How do you sum a dictionary
How do you sum a dictionary  Hi, I want to sum the values of two dictionary based on keys. How do you sum a dictionary
Advertisements
How to return dictionary keys as a list in Python
How to return dictionary keys as a list in Python  Hi, I want to access the keys of dictionary as list in Python program. Here is dictionary data...:"Seven", 8:"Eight", 9:"Nine", 10:"Ten"} print(word_dict) How to get all keys
How to return dictionary keys as a list in Python
How to return dictionary keys as a list in Python  Hi, I want to access the keys of dictionary as list in Python program. Here is dictionary data...:"Seven", 8:"Eight", 9:"Nine", 10:"Ten"} print(word_dict) How to get all keys
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   
Python sum dictionary values by key
Python sum dictionary values by key  Hi, In python we can easily create dictionary and store the values in it. In dictionary we store the values in key value pair. I have two dictionary with same keys and different numeric
combine two pdf files
combine two pdf files       In this program we are going to tell you how you can read a pdf file and combine more than one pdf into one.  To make a program over this, firstly
ModuleNotFoundError: No module named 'map-dictionary-keys'
named 'map-dictionary-keys' How to remove the ModuleNotFoundError: No module named 'map-dictionary-keys' error? Thanks   Hi...ModuleNotFoundError: No module named 'map-dictionary-keys'  Hi, My
combine two arrays in php
combine two arrays in php  combine two arrays in php   $newArray = array_combine($diff, $pages_name['data']); foreach ($newArray as $data) { var_dump($data); exit('zzzzz'); echo $data . '<br>
How to get Keys and Values from HashMap in Java?
How to get Keys and Values from HashMap in Java? Example program of iterating... to iterate the keys and get the values and print on the console. How to get keys... to iterate through keys and get the values. So, let's see how to iterate
Write the Keys and Values of the Properties files in Java
will learn how to add the keys and it's values of the properties files in Java. You.... But this time you will learn, how to insert the keys and values in the properties... Write the Keys and Values of the Properties files in Java
How do I convert a dictionary to a JSON object in Python?
How do I convert a dictionary to a JSON object in Python?  Hi, I... is returning the dictionary object. Now our requirement is to convert it to JSON... in the JSON document. I want to the easy way to convert the dictionary object
how do i begin a two dimensional array?
how do i begin a two dimensional array?  I'm new to java programming and need to create a two dimensional array that enters exactly what is entered in the first dimension and then the first non-white space character of what
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.... But Don't have any idea how can i do that. Like what format of file i need, or how
How do you do data mining projects?
How do you do data mining projects?  Hi, I am beginner in Data... do you do data mining projects? Try to provide me good examples or tutorials links so that I can learn the topic "How do you do data mining projects
how to do two database tables in one page?
how to do two database tables in one page?  dear all: i want to show these two database tables in one page. one table on the left (dbtable.jsp) and the other on the right (table2.jsp). how can i do that with HTML? this is my
Get All Keys and Values of the Properties files in Java
to be inserting in the properties files. Here, you will get the all keys and values...Get All Keys and Values of the Properties files in Java       In this section, you will learn how
Get All Keys and Values of the Properties files in Java
: Here, you will get all keys and values of the properties files with the help of this program. The program shows the keys and values when you run it. ... Get All Keys and Values of the Properties files in Java
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
Combine String example
;  In this section, you will learn how to combine or merge two strings in java. The java.lang package provides the method that helps you to combine two strings and making  into single string. The following
Combine Two Tables(Purchase and Sales) and getting Current Stock
Combine Two Tables(Purchase and Sales) and getting Current Stock  How can i combine both the Tables and Getting Current Stock
Write Keys and Values to the Properties file in Java
how to write keys and values in the properties files through the Java program. The program for writing keys and values to the properties file has been mentioned... Write Keys and Values to the Properties file in Java
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 do you add a numerical value to a regex
How do you add a numerical value to a regex  how do you add a numerical value in a regex. I want to replace my pics ex: bob.jpg susan.jpg mike.jpg with 1.jpg 2.jpg 3.jpg etc. Thanks
How do you master python in a month?
How do you master python in a month?  Hi, I am beginner in Data...: How do you master python in a month? Try to provide me good examples or tutorials links so that I can learn the topic "How do you master python in a month
How do you get the size of an array in Python
How do you get the size of an array in Python  Hi, I want to get the size of my array in Python. How do you get the size of an array in Python? How... array you can use len() function. The len() function takes array an argument
Are you a self-made data scientist? How did you do it?
Are you a self-made data scientist? How did you do it?  Hi, I am... for the tutorials to learn: Are you a self-made data scientist? How did you do it? Try...;Are you a self-made data scientist? How did you do it?". Also tell me
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
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
How can you add elements of two arrayLists in java?
How can you add elements of two arrayLists in java?  if you have... of tempArray2 = [a, b, c, d, e] and you want it to result in newArr = [hi a, my b, name c, is d, john e] how do you go about
How do you map Java Objects with Database tables?
How do you map Java Objects with Database tables?  Hi, How do you map Java Objects with Database tables? thanks

Ads