This article will give some solutions to fix AttributeError: ‘dict_keys’ object has no attribute ‘remove’ in Python. Follow it and solve your problem.
What causes the AttributeError: ‘dict_keys’ object has no attribute ‘remove’ error?
Dictionary is a data type in Python. Dictionary is a list of elements with two fields: key and value. Each key-value pair can be considered an item. ‘Value’ can be any value. ‘Key’ must be an immutable data type such as string, number, or tuple when it is passed to ‘item’.
AttributeError in Python is an error when you try to access a property or a method that does not exist for a particular object.
The error occurs because you call the remove() method in the dictionary instead of calling it on a list.
Example:
myInfor = { 'name': 'John', 'age': 18, 'vacations': 'Dev' } # Use the dict.keys() method to get keys in the dictionary keysInDict = myInfor.keys() # Use the remove() method on the dictionary will throw an error print(keysInDict.remove())
Output:
Traceback (most recent call last):
File "d:\vscode\Python\tempCodeRunnerFile.py", line 11, in <module>
print(keysInDict.remove())
^^^^^^^^^^^^^^^^^
AttributeError: 'dict_keys' object has no attribute 'remove'
How to solve the AttributeError: ‘dict_keys’ object has no attribute ‘remove’ error?
Convert dictionary keys to a list.
Example:
- Use the keys function to retrieve dictionary keys.
- Convert dictionary keys to list and use the remove() function.
myInfor = { 'name': 'John', 'age': 18, 'vacations': 'Dev' } # Use the dict.keys() method to get keys in the dictionary keysInDict = myInfor.keys() print(keysInDict) print(type(keysInDict)) # Use the list() function to convert to a list listKeys = list(keysInDict) print(listKeys) print(type(listKeys)) # Use the remove() method to remove elements in a list listKeys.remove('name') print(listKeys)
Output:
dict_keys(['name', 'age', 'vacations'])
<class 'dict_keys'>
['name', 'age', 'vacations']
<class 'list'>
['age', 'vacations']
Delete directly from the dictionary.
If you don’t want to convert the dictionary to a list, the dictionary data type also has quite a few delete functions so that you can perform delete operations directly on it.
Example:
- Create a dictionary object.
- The pop() function to delete items with a specified key.
myInfor = { 'name': 'John', 'age': 18, 'vacations': 'Dev' } # The pop() function to delete items with a specified key myInfor.pop('name') print(myInfor)
Output:
{'age': 18, 'vacations': 'Dev'}
Or you can use the del keyword to do the same thing.
Example:
myInfor = { 'name': 'John', 'age': 18, 'vacations': 'Dev' } # The del keyword deletes the item with the specified key del myInfor['name'] print(myInfor)
Output:
{'age': 18, 'vacations': 'Dev'}
Note: There is also the dict.popitem() function to delete the last element of the dictionary and the clear() function to delete all the items in the dictionary. You can use it.
Summary:
In conclude, this article have shown you the causes with several ways to solve the error AttributeError: ‘dict_keys’ object has no attribute ‘remove’ in Python. I hope you have an idea of how to fix it. Depending on your situation, you can convert it to a list or delete it directly from the dictionary. If you have any questions, please leave a comment below.
Maybe you are interested:
- AttributeError: ‘list’ object has no attribute ‘lower’
- AttributeError: ‘list’ object has no attribute ‘len’
- Module ‘csv’ has no attribute ‘reader’ in Python

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java