How do I copy values from one dictionary to a second dictionary with new keys?
<p>I am trying to make weapons in my game exclusive to certain classes. I have an item database in form:</p>

<pre><code>itemsList = {
1: {"name": "Padded Armor",  "armor": 1,  "value": 5, "class": "light"},
2: {"name": "Leather Armor",  "armor": 2,  "value": 10, "class": "light"},
3: {"name": "Studded Leather Armor",  "armor": 3,  "value": 25, "class": "light"},
...
19: {"name": "Dagger", "damage" : int(random.randrange(1, 4)), "value": 2, "Type": "Dagger"},
20: {"name": "Dagger + 1", "damage" : int(random.randrange(1, 4) + 1), "value": 200, "Type": "Dagger"},
21: {"name": "Dagger + 2", "damage" : int(random.randrange(1, 4) + 2), "value": 750, "Type": "Dagger"},
22: {"name": "Dagger + 3", "damage" : int(random.randrange(1, 4) + 3), "value": 2000, "Type": "Dagger"}
}
</code></pre>

<p>I am going to import, if a class can equip a certain weapon, to a new dictionary, </p>

<pre><code>character.canEquipWeapon = {}
</code></pre>

<p>I would like to assign the name, damage, value, and type to a new set of keys in the dictionary, only, I would like to add only certain subsets (daggers, maces, swords) to certain classes. </p>

<p>I have tried </p>

<pre><code>character.canEquipWeapon.update(itemsList[5])
</code></pre>

<p>and that just overwrites the dictionary with each new value. How do I go about doing this?</p>

<p>You could create a dict with usable items by class, so that for a given class, you have the list of the IDs it can equip as such :</p>

<pre><code>classItem = {
          'rogue'   : [1,2,3,12,13,14,15], 
          'mage'    : [13,15,16,18], 
          'soldier' : [1,2,3,4,5,6,7],
         }
</code></pre>

<p>Then to get the list of items usable by the rogue for instance :</p>

<pre><code>rogueItems = [v for k,v in itemsList.items() if k in classItem['rogue']]
</code></pre>

<p>Note that with python2 you'd have to use <code>itemsList.iteritems()</code> instead of <code>itemsList.items()</code></p>

python dictionary key