AttributeError: 'NoneType' object has no attribute 'append'

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# AttributeError: 'NoneType' object has no attribute 'append'

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 a function that doesn't return anything.

To solve the error, make sure to only call append() on list objects.

attributeerror nonetype object has no attribute append

Here is a very simple example of how the error occurs.

main.py
my_list = None # ⛔️ AttributeError: 'NoneType' object has no attribute 'append' my_list.append('bobbyhadz.com') # ✅ 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')

checking if the variable is not none

If you need to check if the variable is not None before calling append(), use an if statement.

main.py
my_list = None if my_list is not None: print('variable is NOT None') my_list.append('hello') else: # 👇️ This runs print('variable is None')

Trying to call the append() method on a None value causes the error.

If you print the variable you are calling 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.

# Functions that don't return anything return None

The most common source of a None value (other than an explicit assignment) is a function that doesn't return anything.

main.py
# 👇️ This function returns None def get_list(): print(['bobby', 'hadz', '.']) # 👇️ None my_list = get_list() # ⛔️ AttributeError: 'NoneType' object has no attribute 'append' my_list.append('com')

Notice that our get_list function doesn't explicitly return a value, so it implicitly returns None.

We assigned the result of calling the function to a variable and tried to call append which caused the error.

The error occurs for multiple reasons:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if a certain condition is met.

# Many built-in methods return 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.

main.py
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.

Avoid assignment with methods that mutate the original list in place, as most of them don't return a value and implicitly return None.

# Checking if a variable is not None before calling append()

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().

main.py
my_list = ['bobby', 'hadz', '.'] if my_list is not None: print('variable is not None') my_list.append('com') else: print('variable is None') print(my_list) # 👉️ ['bobby', 'hadz', '.', 'com']

The if block will run only if the my_list variable does not store a None value, otherwise the else block runs.

Alternatively, you can check if the variable stores a list before calling list.append().

main.py
my_list = ['bobby', 'hadz', '.'] if isinstance(my_list, list): print('variable is not None') my_list.append('com') else: print('variable is None') print(my_list) # 👉️ ['bobby', 'hadz', '.', 'com']

The isinstance function returns True if the passed-in object is an instance or a subclass of the passed-in class.

main.py
my_list = ['bobby', 'hadz', '.'] print(isinstance(my_list, list)) # 👉️ True print(isinstance(my_list, str)) # 👉️ False

# A function that returns a value only if a condition is met

Another common cause of the error is having a function that returns a value only if a condition is met.

main.py
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 run if the passed-in list has a length greater than 3.

In all other cases, the function doesn't return anything and ends up implicitly returning 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.

main.py
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 of whether the condition is met.

# The list.append() method should only be called on a list

The list.append() method adds an item to the end of the list.

main.py
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # 👉️ ['bobby', 'hadz', 'com']

The method returns None as it mutates the original list.

If the error persists, follow the instructions in my AttributeError: 'NoneType' object has no attribute 'X' article.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.