+ 1

Dictionary

i have 2 dic, a and b a = {'a':100, 'b':200, 'c':300} b = {'a':300, 'b':200, 'd':400} i want to create new dic, dic c how to create dic c, if key in a and b same, value will be added, like this: c = {'a':400, 'b':400, 'c':300, 'd':400}

23rd Nov 2017, 3:01 AM
Razka Agniatara
Razka Agniatara - avatar
2 Answers
+ 4
c = {'a': a['a'] + b['a'], 'b': a['b'] + b['b'], 'c': a['c'], 'd': b['d']} should work. Unless you want to use a for loop. 😉
23rd Nov 2017, 3:06 AM
blackcat1111
blackcat1111 - avatar
23rd Nov 2017, 6:47 AM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar