Borislav Hadzhiev
Wed Apr 20 2022·3 min read
Photo by Hernan Sanchez
The Python "AttributeError: 'NoneType' object has no attribute 'append'"
occurs when we try to call the append()
method on a None
value, e.g.
assignment from function that doesn't return anything. To solve the error, make
sure to only call append()
on list objects.
Here is a very simple example of how the error occurs.
my_list = None # ⛔️ AttributeError: 'NoneType' object has no attribute 'append' my_list.append('hello') # ✅ if you need to check if not None before calling append() if my_list is not None: print('variable is NOT None') my_list.append('hello') else: print('variable is None')
Trying to call the append()
method on a None
value is what causes the error.
append()
on, it will be None
, so you have to track down where the variable gets assigned a None
value and correct or remove the assignment.The most common source of a None
value (other than an explicit assignment) is
a function that doesn't return anything.
# 👇️ this function returns None def get_list(): print(['a', 'b', 'c']) # 👇️ None my_list = get_list() # ⛔️ AttributeError: 'NoneType' object has no attribute 'append' my_list.append('d')
Notice that our get_list
function doesn't explicitly return a value, so it
implicitly returns None
.
append
which caused the error.The "AttributeError: 'NoneType' object has no attribute 'append'" occurs for multiple reasons:
None
implicitly).None
.Some built-in methods (e.g. sort
) mutate a data structure in place and don't
return a value. In other words, they implicitly return None
.
my_list = ['c', 'b', 'a'] my_sorted_list = my_list.sort() print(my_sorted_list) # 👉️ None # ⛔️ AttributeError: 'NoneType' object has no attribute 'append' my_sorted_list.append('d')
The sort()
method sorts a list in place and doesn't return anything, so when
we assign the result of calling the method to a variable, we assign a None
value to the variable.
None
.If a variable might sometimes store a list and sometimes store None
, you can
explicitly check if the variable is not None
before you call append()
.
my_list = ['a', 'b', 'c'] if my_list is not None: print('variable is not None') my_list.append('d') else: print('variable is None') print(my_list) # 👉️ ['a', 'b', 'c', 'd']
The if
block will run only if the my_list
variable does not store a None
value, otherwise the else
block runs.
Another common cause of the error is having a function that returns a value only if a condition is met.
def get_list(a): if len(a) > 3: return a my_list = get_list(['a', 'b']) print(my_list) # 👉️ None
The if
statement in the get_list
function is only ran if the passed in list
has a length greater than 3
.
None
.To solve the error in this scenario, you either have to check if the function
didn't return None
or return a default value if the condition is not met.
def get_list(a): if len(a) > 3: return a return [] my_list = get_list(['a', 'b']) print(my_list) # 👉️ []
Now the function is guaranteed to return a list regardless if the condition is met.