insert key-pair value into nested dict without overwriting after delimiter of key which produce duplicate key
<p>Assume that I have a nested dict like:</p>

<pre><code>D={'Germany': {'1972-05-23': 'test1', '1969-12-27': 'test2'},
   'Morocco|Germany': {'1978-01-14':'test3'}}
</code></pre>

<p>I want to get a new dict like:</p>

<pre><code>{'Germany': {'1972-05-23': 'test1', '1969-12-27': 'test2', '1978-01-14':'test3'}
 'Morocco': {'1978-01-14':'test3'}}
</code></pre>

<p>which means I have to handle the perhaps duplicate key after the <code>str.split(key)</code> ,and this is my code:</p>

<pre><code>D={'Germany': {'1972-05-23': 'test1', '1969-12-27': 'test2'},
   'Morocco|Germany': {'1978-01-14':'test3'}}

new_dict={}
for item in D:
    for index in str.split(item,'|'):
        new_dict[index]=D[item]
print new_dict
</code></pre>

<p>however the key-value pair generated after splitting operation overwriting the original ones which result:</p>

<pre><code>{'Morocco': {'1978-01-14': 'test3'}, 'Germany': {'1978-01-14': 'test3'}}
</code></pre>

<p>I wonder how I can modify the my code to get a satisfying dict for further processing or any better solution for this requirement?</p>

<p>PS: My Python Version is 2.7.12 with Anaconda 4.0.0 via IDE PyCharm</p>

<p>Any help will be appreciated, thank you</p>

<p>you could use:</p>

<pre><code>if not index in new_dict: new_dict[index] = {}
new_dict[index].update(D[item])
</code></pre>

python dictionary split overwrite