Inazuma
Inazuma

Reputation: 178

Adding items to dictionary from a dictionary

I want to copy an item from one list to another. For example:

chest = {}

chest["sword":1,3]
chest["gold":5]

inventory = {}

inventory.add(chest["sword"])    #example pseudo code

How would I add 1 item from "chest" to "inventory", keeping the key name, and values?

Thanks for any help!

EDIT TO AVOID CONFUSION:

I want to be able to add a SINGLE item from, say "chest" to "inventory". so:

inventory = {}
chest = {"sword":1, "gold":5, "other item":"other data"}

I want to take 1 item from "chest" (depending on user input) and add it to "inventory":

inventory.add( chest [ item_from_chest ] ) #exampe pseudo code

after this inventory world look like this:

>>>{"sword":1}

Sorry for any confusion, hope this edit helps!

Upvotes: 0

Views: 102

Answers (2)

Hyperboreus
Hyperboreus

Reputation: 32459

Most probably you will need some merging algorithm in order not to overwrite existing items in you inventory. Something like:

#! /usr/bin/python3.2

import random

prefices = ['', 'Shabby', 'Surreal', 'Gleaming', 'Muddy']
infices = ['Sword', 'Banana', 'Leather', 'Gloves', 'Tea Cup']
suffices = ['', 'of the Bat', 'of Destruction', 'of Constipation', 'of thy Mother']

def makeItem ():
    tokens = [random.choice (fix) for fix in (prefices, infices, suffices) ]
    return ' '.join (t for t in tokens if t)

def makeChest ():
    chest = {'Gold': random.randint (5, 20) }
    for _ in range (random.randint (0, 5) ):
        chest [makeItem () ] = 1
    return chest

inventory = {}
while True:
    chest = makeChest ()
    print ('\n\n\nThou hast found a chest containing: {}'.format (', '.join ('{} {}'.format (v, k) for k, v in chest.items () ) ) )
    r = input ('\nWhishest thou to pick up this apparel? [y/*] ')
    if r not in 'Yy': continue

    for k, v in chest.items ():
        try: inventory [k] += v
        except KeyError: inventory [k] = v

    print ('\n\nThy bag now containth:')
    for k, v in inventory.items ():
        print ('    {:6d} {}'.format (v, k) )

Example Session:

Thou hast found a chest containing: 13 Gold

Whishest thou to pick up this apparel? [y/*] y


Thy bag now containth:
        13 Gold



Thou hast found a chest containing: 1 Shabby Gloves of Destruction, 1 Surreal Sword, 1 Gleaming Gloves of Constipation, 7 Gold

Whishest thou to pick up this apparel? [y/*] y


Thy bag now containth:
         1 Shabby Gloves of Destruction
         1 Surreal Sword
         1 Gleaming Gloves of Constipation
        20 Gold

As you see the gold got added. Also if you find another Surreal Sword, you will have 2 in your inventory.

Upvotes: 2

alecxe
alecxe

Reputation: 474271

You can use dictionary update():

Update the dictionary with the key/value pairs from other, overwriting existing keys.

>>> chest = {}
>>> chest = {"sword": 1, "gold": 5}
>>> inventory = {}
>>> inventory.update(chest)
>>> inventory
{'sword': 1, 'gold': 5}

Or, if you need just one sword item, simply:

>>> inventory['sword'] = chest['sword']
>>> inventory
{'sword': 1}

Or, if there are several keys you need to copy (but not all of them):

>>> keys_to_copy = ['sword']
>>> inventory.update(((key, chest[key]) for key in keys_to_copy))
>>> inventory
{'sword': 1}

Upvotes: 1

Related Questions