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__'
Sina
  • 431
  • 4
  • 7
  • ```del inventory["backpack"][1]``` works fine for me. – Danstahr Oct 21 '13 at 13:03
  • This made me this error : Traceback (most recent call last): File "python", line 15, in TypeError: 'NoneType' object does not support item deletion – Sina Oct 21 '13 at 13:05

1 Answers1

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.

Tom Ron
  • 5,906
  • 3
  • 22
  • 38