6

I have a dictionary like this:

inventory = {'gold' : 500, 
        'pouch' : ['flint', 'twine', 'gemstone'], 
        'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']}

How can I remove the dagger from it ?

I tried this:

inventory["backpack"][1].remove()

or

del inventory["backpack"][1]

but It made this error:

Traceback (most recent call last):
File "python", line 15, in <module>
TypeError: 'NoneType' object has no attribute '__getitem__'
2
  • del inventory["backpack"][1] works fine for me. Commented Oct 21, 2013 at 13:03
  • This made me this error : Traceback (most recent call last): File "python", line 15, in <module> TypeError: 'NoneType' object does not support item deletion Commented Oct 21, 2013 at 13:05

1 Answer 1

1

inventory["backpack"][1].remove() - applied remove on inventory["backpack"][1] which is a string and has no remove attribute.

You can also use slice to delete it-

inventory["backpack"] = inventory["backpack"][:1] + inventory["backpack"][2:]

or -

inventory["backpack"].remove(inventory["backpack"][1])

Same follows for - del inventory["backpack"][1]. You apply del on list object but it does not have such an attirbute.

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

1 Comment

Obvious to me is inventory['backpack'].remove('dagger')

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.