Identifying and adding values from dict to another dict, while both contain the similar keys
<p>Let's say I had these two dictionaries, both containing similar keys to each other:</p>

<pre><code>d_1 = {'a': 3, 'b': 4, 'c': 1}

d_2 = {'a': 5, 'b': 6, 'c': 9, 'd': 7}
</code></pre>

<p>Now, let's say I want to take the values of <code>d_1</code> and add them to the corresponding values of <code>d_2</code>. How would I add the values of <code>d_1</code>'s <code>a</code> key to the respective key in <code>d_2</code> w/o individually saying <code>d_2['a'] += d_1['a']</code> for each key? How could I go about writing a function that can take two dicts, compare their keys, and add those values to the identical existing key values in another dict?  </p>

<p>For example, if I had a class named <code>player</code> and it has an attribute of <code>skills</code> which contains several skills and their values, e.g. <code>strength: 10</code> or <code>defense: 8</code>, etc.  Now, if that <code>player</code> were to come across an armor set, or whatever, that buffed specific skills, how could I take that buff dictionary, see what keys it has in common with the <code>player</code>'s <code>skills</code> attribute, and add the respective values?</p>

<p>For every key in d_2, check if them in d_1, if so, add them up.</p>

<p><code>for key in d_2:
    if key in d_1:
        d_2[key] += d_1[key]</code></p>

<p>It seems you want to built a game, then you might not want to mess with the player's attribute. add them up and put the outcome to a new dict would be better. What's more, you can add more than two dict together, as you might have more than one buff.</p>

<p><code>for key in player:
    outcome[key] = 0
    for buff in buffs:
        if key in buff:
            outcome[key] += buff[key]
    outcome[key] += player[key]</code></p>

python dictionary