AttributeError: 'list' object has no attribute 'X'
1 · The lesson
readWhat this error means
You called a method on a list that lists simply don't have. Either you misremembered the method name, you're translating from another language (JavaScript's .push, Java's .add, C#'s .Length), or the variable isn't the list-of-objects you think it is — it's a list, but you wanted to call the method on one of its elements.
When you see it
Traceback (most recent call last): File "main.py", line 3, in <module> items.add(42) AttributeError: 'list' object has no attribute 'add'
Why it happens
Two flavours:
1. Wrong method name. Python lists use .append, .extend, .insert, .remove, .pop, .sort, .reverse. They don't have .add, .push, .length, .size, or .contains.
2. Wrong level. You have a list of objects but you're calling the object's method on the list. users.upper() doesn't work; users[0].upper() does.
How to fix it
Quick translation table for newcomers:
| You wanted (other language) | Python equivalent |
|---|---|
arr.push(x) (JS), list.add(x) (Java) | arr.append(x) |
arr.pop() end | arr.pop() (same) |
arr.length (JS), arr.Length (C#), arr.size() (Java) | len(arr) |
arr.contains(x) (Java) | x in arr |
arr.indexOf(x) (JS) | arr.index(x) |
arr.concat(other) (JS) | arr + other or arr.extend(other) |
arr.includes(x) (JS) | x in arr |
arr.toString() | str(arr) or ",".join(arr) |
arr.forEach(fn) | for x in arr: fn(x) |
arr.map(fn) | [fn(x) for x in arr] |
arr.filter(fn) | [x for x in arr if fn(x)] |
arr.reduce(fn, init) | functools.reduce(fn, arr, init) |
Loop or comprehension when the method belongs to the elements:
names = ["alice", "bob"] # names.upper() # AttributeError shouty = [n.upper() for n in names]
Use len() not .length:
items = [1, 2, 3] print(len(items)) # not items.length
When you'd actually see this in real code
- A JS developer's first day in Python:
items.push(x). - A Java developer:
items.size(). - Pandas DataFrame method called on a list:
df_list.head()won't work ifdf_listis a Python list, not a DataFrame. - Forgetting to unwrap a result:
result.split(",")whenresultis already a list of strings.
Related errors
- AttributeError: 'NoneType' object has no attribute 'X' — same error family, different cause.
- TypeError: list indices must be integers or slices, not str — also a list/dict confusion.
- AttributeError: module 'X' has no attribute 'Y'
See Also
- All Python errors — the full index, by type and by when it happens.
- Lists (tutorial)
- List methods cheat sheet
- List comprehensions