0

Here is my dictionary:

inventory = {
    'gold' : 500,
    'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key
    'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']
}

I need to add 50 to the index of gold. What should I do? I tried:

inventory['gold'].append(50)

3 Answers 3

2

gold is not a list. It is an integer, so you use addition:

inventory['gold'] += 50

This uses augmented assignment, which for integers is equivalent to:

inventory['gold'] = inventory['gold'] + 50

If you need gold to be a list as well, and want to end up with [500, 50] as the value, you'll have to replace the current value with a list:

inventory['gold'] = [inventory['gold'], 50]

If you need to add multiple values over time, and don't know if gold is a list or a simple integer, and cannot change the original dictionary to always use a list, you could use exception handling:

try:
    inventory['gold'].append(50)
except AttributeError:
    # not a list yet
    inventory['gold'] = [inventory['gold'], 50]

It would be far easier to maintain your project if you started with gold always being a list object, however.

Sign up to request clarification or add additional context in comments.

9 Comments

Downvoted as the question was edited to match this answer, with my answer embedded into this one in case that op didn't mean addition. I'm completely pro a complete answer. Which is what you've now got, but the edit to the question made it look shady. - Otherwise I wouldn't mind
@Scironic: I always expand answers to cover all bases I think of. I did not need to consult your answer to do so. You are free to update your answer too. Personally, I use voting to reflect the helpfulness of answers, not for perceived plagiarism, that is what flagging is for.
I understand that, I do the same. My point was that you edited the question to suit your answer more favourably (with or without intention to do so).
@Scironic: the goal here is to provide good answers to questions. I'm sorry that you feel something shady is going on, but I can assure you I make all my edits on my own terms.
@Scironic: wait, you said edited the question to match my answer?! I thought you were talking about my edits to my own answer here. I always clean up questions, make editorial edits (spelling, grammar, formatting, clarity). You are encouraged to do so (there is a series of badges for that). I certainly did not edit it to match my answer. Where do you see that?
|
2

Assuming you mean you want to append 50 to gold. Make gold a list:

inventory = {
    'gold' : [500],
    'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key
    'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']
}

inventory['gold'].append(50)

If you meant add, use Martijn's solution.

Comments

0

If you want to add 60 to the value of the key "Platinum"

inventory ["Platinum"] += 60

If you want to keep the value of 500 but also have a value of 60 you need something to contain them both such as a list.

You could initialize your "Platinum" value with a list and then append 500 and then 60 to it.

inventory ["Platinum"] = list ()
inventory ["Platinum"].append (500)
inventory ["Platinum"].append (60)

Or you could use the defaultdict to make it slightly simpler.

from collections import defaultdict


inventory = defaultdict (list)  # every missing value is now a list.

inventory ["Platinum"].append (500) # add 500 to the list.
inventory ["Platinum"].append (60) # add 60 to the list.
inventory ["pouch"] = ['Flint', 'twine', 'gemstone'] # create a new key with a new value.
inventory['animals'].extend(['Elephant', 'dog','lion']) # extend list to include.
inventory['pouch'].remove('Flint')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.