AttributeError: 'list' object has no attribute
What it means
You called a method that lists do not have. Often the method belongs to the items inside the list, like split() or lower() on strings, so you need to call it on each item. The name at the end of the message is the method you tried.
1. Calling a string method on a whole list
This raises the error
Traceback (most recent call last): File "<exec>", line 2, in <module> AttributeError: 'list' object has no attribute 'split'
The fix
['Ana', '90'] ['Ben', '85']
split() belongs to strings, and lines is a list of strings. Loop over the list, or use a comprehension like [line.split(',') for line in lines].
2. Using method names from another language
This raises the error
Traceback (most recent call last): File "<exec>", line 2, in <module> AttributeError: 'list' object has no attribute 'push'
The fix
4
Python lists use append() to add an item and the len() function for their size. JavaScript's push() and .length do not exist here.
3. A list of dictionaries treated as one dictionary
This raises the error
Traceback (most recent call last): File "<exec>", line 2, in <module> AttributeError: 'list' object has no attribute 'get'
The fix
Ana Ana Ben
.get() belongs to each dictionary, not to the list holding them. Pick one with people[0], or loop over them.
When you see this error, check
- Print the variable and its
type()to confirm it is a list. - Run
print(dir([]))to see every method a list has. - If the method belongs to the items, call it inside a loop or a comprehension.
- Check whether a function like
split()orre.findall()already turned your value into a list.
Related errors
All Python errors · How to read a traceback · Step through your code