Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can you get the name of an object in a dictionary?

weapons = {
    'sword' : {
        'damage' : 10,
        'type_damage' : 'sharp',
        'max_durability' : 20
    },
    'spear' : {
        'damage' : 7,
        'type_damage' : 'pointy',
        'max_durability' : 25
    },
    'log' : {
        'damage' : 5,
        'type_damage' : 'blunt',
        'max_durability' : 15
    }
}
inventory = {
    
}

inventory.update(weapons['sword'])
print(inventory)

I want to also add the ‘sword’ to inventory, not just the arguments, but I can’t figure out how.

I want it to output

{'sword' : {'damage': 5, 'type_damage': 'blunt', 'max_durability': 15}}

but instead it gives

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{'damage': 5, 'type_damage': 'blunt', 'max_durability': 15}

>Solution :

update function returns only the value of a key-value pair.

To assign the key as well, you can do it like this:

weapons = {
    'sword' : {
        'damage' : 10,
        'type_damage' : 'sharp',
        'max_durability' : 20
    },
    'spear' : {
        'damage' : 7,
        'type_damage' : 'pointy',
        'max_durability' : 25
    },
    'log' : {
        'damage' : 5,
        'type_damage' : 'blunt',
        'max_durability' : 15
    }
}
inventory = {
    
}

inventory.update({'sword': weapons['sword']})
print(inventory)

Output:

{'sword': {'damage': 10, 'type_damage': 'sharp', 'max_durability': 20}}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading