Update/Append to dictionary
<p>Let's say I have a dictionary like so:</p>

<pre><code>x = {'age': 23,
     'channel': ['a'],
     'name': 'Test',
     'source': {'data': [1, 2]}}
</code></pre>

<p>and a similar one like:</p>

<pre><code>y = {'age': 23,
     'channel': ['c'],
     'name': 'Test',
     'source': {'data': [3, 4], 'no': 'xyz'}}
</code></pre>

<p>if I used this <code>x.update(y)</code> I would lose <code>'channel'</code> previous info for example.. How can I append the values when they are different and add keys/values when they are not already in the dictionary?</p>

<p>End result should be:</p>

<pre><code>{'age': 23,
 'channel': ['a', 'c'],
 'name': 'Test',
 'source': {'data': [1, 2, 3, 4], 'no': 'xyz'}}
</code></pre>

<p>I came close with this:</p>

<pre><code>for a,b in y.iteritems():
    try:
        x[a] = x[a] + y[a]
    except:
        x[a] = y[a]
</code></pre>

<p>but it failed when it found a dictionary inside of the dictionary.</p>

<p>You can access the value associated to the key and make the changes.
Lets take the dict example you have taken in your question:</p>

<pre><code>x = {'name': 'Test', 'age': 23, 'channel': ['a'], 'source': {'data':   [1,2]}}
x['channel']=['a']
</code></pre>

<p>The value is of type list[].
you can append or add values to a list by list.append()</p>

<p>So, <code>x['channel'].append('c')</code> will give you:</p>

<pre><code>{'age': 23, 'channel': ['a', 'c'], 'name': 'Test', 'source': {'data': [1, 2]}}
</code></pre>

python dictionary