"AttributeError: ‘NoneType’ Object Has No Attribute ‘remove’."


#1

I was stuck in changing the value of the dictionary.
I tried to replace the old value which is inventory[‘backpack’] to what we want ---- inventory[‘backpack’].sort(), so I used the code as follow:
inventory[‘backpack’] = inventory[‘backpack’].sort()
inventory[‘backpack’] = inventory[‘backpack’].remove(‘dagger’)
But it kept reminding me that AttributeError: ‘NoneType’ object has no attribute ‘remove’.
I appreciate for any help.


FAQ: Learn Python: Python Lists and Dictionaries - It's Dangerous to Go Alone! Take This
#2

Hi @z977wang,

Could you please post your full code?


#3

https://www.codecademy.com/courses/learn-python/lessons/python-lists-and-dictionaries/exercises/its-dangerous-to-go-alone-take-this?action=resume_content_item

This line could be the cause of your problem. list.sort() has no return value, hence, None, and the cause of the noneType error.

Either sort in place,

list.sort()

or assign a sorted list,

new_list = sorted(old_list)

#4

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