Q. Write a Python program to combine two dictionary adding values for common keys.

 

d1 = {'a': 100, 'b': 200, 'c': 300}

d2 = {'a': 300, 'b': 200, 'd': 400}

 

Sample output:

Counter ({'a': 400, 'b': 400, 'c': 300, 'd': 400})

 

Answer =

 



d1 = {'a': 100, 'b': 200, 'c': 300}
d2 = {'a': 300, 'b': 200, 'd': 400}

for i in d2 :
    if i in d1 :
        d1[ i ] = d1[ i ] +d2[ i ]
    else :
        d1[ i ] = d2[ i ]
print(d1)

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post