it seems a simple task:
I am trying to merge 2 dictionaries without overwriting the values but APPENDING.
a = {1: [(1,1)],2: [(2,2),(3,3)],3: [(4,4)]}
b = {3: [(5,5)], 4: [(6,6)]}
number of tuples a = 4, number of tuples b = 2
This is why I have singled out these options since they are overwriting:
all = dict(a.items() + b.items())
all = dict(a, **b)
all = a.update([b])
The following solution works just fine, BUT it also appends values to my original dictionary a:
all = {}
for k in a.keys():
if k in all:
all[k].append(a[k])
else:
all[k] = a[k]
for k in b.keys():
if k in all:
all[k].append(b[k])
else:
all[k] = b[k]
Output =
a = {1: [(1, 1)], 2: [(2, 2), (3, 3)], 3: [(4, 4), **[(5, 5)]**]}
b = {3: [(5, 5)], 4: [(6, 6)]}
c = {1: [(1, 1)], 2: [(2, 2), (3, 3)], 3: [(4, 4), [(5, 5)]], 4: [(6, 6)]}
number of tuples a = 5 !!!!!, number of tuples b = 2 (correct), number of tuples all = 6 (correct)
It appended tuple [(5,5)]
from b to a. I have no idea as to why this happens because all I am coding is to write everything into the complete dictionary "all".
Can anyone tell me where the heck it is changing dict(a) ???????
Any help is greatly welcome.