Merging Dicts that have Lists of Dicts in Python 2.7
<p>I am trying to merge two <code>dict</code>s in Python that could be The same or one could have far less info </p>

<p>ex.</p>

<pre><code>master = {"a": 5564, "c": [{"d2":6}]}
daily = { "a": 795, "b": 1337, "c": [{"d1": 2,"d2": 2,"d3": [{"e1": 4,"e2": 4}]}]}
</code></pre>

<p>They need to be merged so the output is as such</p>

<pre><code>master = { "a": 6359, "b": 1337, "c": [{"d1": 2,"d2": 8,"d3": [{"e1": 4,"e2": 4}]}]}
</code></pre>

<p>I took a shot at it though I only ever get returned null. I might be missing something or just way off. I just cant figure it out. Any help would be amazing Thank you. </p>

<pre><code>def merge(master,daily):
    for k, v in daily.items():
        if isinstance(daily[k],list):
            key_check = keyCheck(k, master)
            if key_check:
                merge(master[k],daily[k])
            else :
                master[k] = daily[k]
        else :
            if keyCheck(k, master):
                 master[k] += daily[k]
            else :
                master[k] = daily[k]
</code></pre>

<p><code>keyCheck</code> only checks if a key is in the dictionary so it doesn't throw errors.</p>

<p>Here is a recursive solution. Though it cannot compete with Kasramvd's answer.</p>

<pre><code>def merge(dic1, dic2):

    merged = dict(dic1, **dic2) # Merge dictionaries without adding values. Just exchanging them.
                                # Similar to .update() but does not override subdicts.

    for key in merged:
        if key in dic1 and key in dic2:
            if isinstance(dic1[key], list):
                merged[key] = merge(dic1[key][0], dic2[key][0])
            else:
                merged[key] = dic1[key] + dic2[key]

    return merged
</code></pre>

python python-2.7 dictionary