13/14


#1

I got the following error with the following code:

CODE: backpack = ['xylophone', 'dagger', 'tent', 'bread loaf']
backpack['dagger'].remove()

ERROR: Traceback (most recent call last):
File "python", line 2, in
AttributeError: 'str' object has no attribute 'remove'

Please help!


#2

Hello, @carson2040,

remove is a method that belongs to the dict type. What this means is that if you want to use remove to get rid of an item in a dictionary, you need to specify the dictionary object, followed by a dot, followed by remove, and finally by parentheses that contain the key of the item you wish to remove.

In this case, the dictionary is backpack and the key of the item you need to get rid of is 'dagger'. So, the line that accomplishes the task is ...

backpack.remove('dagger')

#3

That helped alot. thanks.


#4

Hello, I did that and I get an error saying (backpack has to be a list! Click Reset Code to get back to the original code.) So I thought backpack['dagger'].remove() would resolve, but I get this error (TypeError: list indices must be integers, not str). Please Help Me.

Thanks


#5

I've face same problem with you.

When i type : backpack.remove('dagger')

Error message appear : "Oops, try again. backpack has to be a list! Click Reset Code to get back to the original code."

When i try : backpack['dagger'].remove()
This error appear in console :

Traceback (most recent call last):
File "python", line 2, in
TypeError: list indices must be integers, not str

any thought?


#6

Should work as long as there is a list called backpack with and element of type string with the value 'dagger'. Reset your code please and retry. It's important to execute backpack.remove('dagger') after the line where you assign the list to the variable called backpack:

backpack = ['dagger','anotheritem','...']
backpack.remove('dagger')

And not:

backpack.remove('dagger') 
backpack = ['dagger','anotheritem','...']

You can't access a list that way. Lists are ordered and each order has a number called index starting at [0..len(list)-1] and are all int numbers. Therefore remove() can only be executed on lists.