+ 5

Dictionary in Python

How can I write the following program: A program that receives two dictionaries and adds the values of two dictionaries whose keys are the same and combines the two dictionaries. Example: A={'1':1 , '2':2 , '3':3} B={'3':3 ,'4':4} Answer={'1':1 ,'2':2 , '3':6 , '4':4}

17th May 2020, 6:28 AM
SUN
SUN - avatar
9 Respostas
+ 1
for i,j in B.items(): if i in A: c=A[i]+B[i] A[i]=c if i not in A: A[i]=j print(A)
17th May 2020, 7:37 AM
Abhay
Abhay - avatar
+ 6
Abhay thanks a lot
17th May 2020, 2:56 PM
SUN
SUN - avatar
+ 3
#Abhay for i,j in B.items(): . . Correct ?
17th May 2020, 7:47 AM
SUN
SUN - avatar
+ 1
viva
23rd Jul 2020, 8:18 PM
Sha’aban Abdulrahman
Sha’aban Abdulrahman - avatar
0
hi, please have a cheatsheet on your desktop. It is a summary of most common statements, functions and datatypes. Google helps you to find one.
17th May 2020, 6:36 AM
Oma Falk
Oma Falk - avatar
0
SUN what do you mean?
17th May 2020, 2:31 PM
Abhay
Abhay - avatar
0
👍
17th May 2020, 2:57 PM
Abhay
Abhay - avatar