Merge dictionaries without overwriting values
<p>it seems a simple task: </p>

<p>I am trying to merge 2 dictionaries without overwriting the values but APPENDING. </p>

<pre><code>a = {1: [(1,1)],2: [(2,2),(3,3)],3: [(4,4)]} 
b = {3: [(5,5)], 4: [(6,6)]}
</code></pre>

<p>number of tuples a = 4, number of tuples b = 2 </p>

<p>This is why I have singled out these options since they are overwriting:</p>

<pre><code>all = dict(a.items() + b.items()) 
all = dict(a, **b)
all = a.update([b])
</code></pre>

<p>The following solution works just fine, BUT it also appends values to my original dictionary a:</p>

<pre><code>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]
</code></pre>

<p>Output =</p>

<pre><code>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)]}
</code></pre>

<p>number of tuples a = <strong>5 !!!!!</strong>, number of tuples b = 2 (correct), number of tuples all = 6 (correct)</p>

<p>It appended tuple <code>[(5,5)]</code> 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".</p>

<p>Can anyone tell me where the heck it is changing dict(a) ???????</p>

<p>Any help is greatly welcome. </p>
