Modify multiple keys of dictionary by a mapping dictionary
<p>I have 2 dict, one original and one for mapping the original one's key to another value simultaneously,for instance:</p>

<p><strong>original dict</strong></p>

<pre><code>built_dict={'China':{'deportivo-cuenca-u20':{'danny':'test1'}},
            'Germany':{'ajax-amsterdam-youth':{'lance':'test2'}}}
</code></pre>

<p><strong>mapping dict</strong></p>

<pre><code>club_team_dict={'deportivo-cuenca-u20':'deportivo','ajax-amsterdam-youth':'ajax'}
</code></pre>

<p>It works well if I use the following code to change the key of the nested dict of original dict,like</p>

<pre><code>def club2team(built_dict,club_team_dict):
for row in built_dict:
    # print test_dict[row]
    for sub_row in built_dict[row]:
        for key in club_team_dict:
            # the key of club_team_dict must be a subset of test_dict,or you have to check it and then replace it
            if sub_row==key:
                built_dict[row][club_team_dict[sub_row]] = built_dict[row].pop(sub_row)
return built_dict
</code></pre>

<p>and the <strong>result</strong></p>

<pre><code>{'Germany': {'ajax': {'lance': 'test2'}}, 'China': {'deportivo': {'danny': 'test1'}}}
</code></pre>

<p>so far so good, however if I have a dict with multiple key mapping to the same key,for example,my original dict is like</p>

<pre><code>built_dict={'China':{'deportivo-cuenca-u20':{'danny':'test1'}},
            'Germany':{'ajax-amsterdam-youth':{'lance':'test2'},
                       'ajax-amsterdam':{'tony':'test3'}}}
</code></pre>

<p>and the mapping dict with more then 1 key mapping to the same value,like:</p>

<pre><code>club_team_dict={'deportivo-cuenca-u20':'deportivo',
                'ajax-amsterdam-youth':'ajax',
                'ajax-amsterdam':'ajax'}
</code></pre>

<p>as you can see, both <code>'ajax-amsterdam-youth'</code>and <code>'ajax-amsterdam-youth'</code> are mapping to <code>'ajax'</code>,and the trouble is when I use the same code to execute it, the original dict's size has been changed during the iteration</p>

<pre><code>RuntimeError: dictionary changed size during iteration
</code></pre>

<p>I want to get a result with nested list for the same key like this</p>

<pre><code>{'Germany': {'ajax':[{'lance': 'test2'},
                     {'tony' : 'test3'}]}},
 'China': {'deportivo': [{'danny': 'test1'}]}}
</code></pre>

<p>Any help will be appreciate,thank you, and I will keep looking for an answer by myself.</p>

<hr>

<p>Well I have found a solution for this,the code:</p>

<pre><code>def club2team(built_dict,club_team_dict):
for row in built_dict:
    # print test_dict[row]
    for sub_row in built_dict[row].keys():
        for key in club_team_dict:
            # the key of club_team_dict must be a subset of test_dict,or you have to check it and then replace it
            if sub_row==key:
                # built_dict[row][club_team_dict[sub_row]] = built_dict[row].pop(sub_row)
                built_dict[row].setdefault(club_team_dict[sub_row],[]).append(built_dict[row].pop(sub_row))
return built_dict
</code></pre>

<p>pay attention to the <code>for sub_row in built_dict[row].keys():</code> and <code>setdefault()</code> method, I used to believe that in <code>python 2.7</code>, the default iteration for dict is just iterate the keys(), however, this time it proves it's a little different, maybe you have better solution, please show me and it will be appreciate,thank you</p>

python dictionary