Error guide · AttributeError
AttributeError: 'NoneType' object has no attribute 'append'
A variable holds None, which means nothing, so the method you called does not exist.
Example
numbers = [3, 1, 2] numbers = numbers.sort() numbers.append(4)
What Python says
AttributeError: 'NoneType' object has no attribute 'append'
As printed by Python 3.12. Other versions may word it slightly differently.
Why it happens
sort(), append(), reverse() and similar methods change the list in place and return None. Writing numbers = numbers.sort() replaces your list with None, so the next method call fails. The same thing happens when a function you wrote has no return and you use its result.
How to fix it
numbers = [3, 1, 2] numbers.sort() numbers.append(4) print(numbers)
Output: [1, 2, 3, 4]
Tips
- Call in-place methods on their own line, without assigning the result.
- To get a new sorted list and keep the old one, use
sorted(numbers). - If a function should give a result back, check that it ends with
return.