Https://discuss.codecademy.com/c/python/python-beginner-en-pwmb1


#1


in d above code we were asked to remove "dagger" . when using del command on selectin index of dagger as 2 it is getting deleted rather we should use index as 1?


inventory = {
    'gold' : 500,
    'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key
    'backpack' : ['xylophone','dagger', 'bedroll','bread loaf'],
    'pocket' : ['seashell','strange berry', 'lint']
}
inventory['backpack'].sort()
del inventory['backpack'][2]
inventory['gold'] = 500+50
# Adding a key 'burlap bag' and assigning a list to it
inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']

# Sorting the list found under the key 'pouch'
inventory['pouch'].sort()


#2

Recall the lists are zero-indexed. Are you saying that index 1 does not delete the correct element?

Should you be using remove instead of del?

Did you accidentally delete the backpack variable? Click Reset Code to get back to the original code.


#3

please tell me how to use remove command and ye i know list are zero indexed but using index as 2 deleted dagger and using 1 as index showed error.


#4

The index doesn't come into question. del removed backpack.

inventory['backpack'].remove('dagger')

I tested your code and it messed up my lesson. Resetting did not get rid of the problem. Hopefully you find a way around this. You might have to go back a few lessons, Reset and redo the lessons leading up to this one.

del and remove are very different from one another. del is for removing variables. remove is for removing items from a list.


#5

okay thank you!:grinning:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.