Select Page

How to Solve Python AttributeError: ‘NoneType’ object has no attribute ‘append’

by | Programming, Python, Tips

If you attempt to call the append() method on a variable with a None value, you will raise the error AttributeError: ‘NoneType’ object has no attribute ‘append’. To solve this error, ensure you are not assigning the return value from append() to a variable. The Python append() method updates an existing list; it does not return a new list.

This tutorial will go through how to solve this error with code examples.


AttributeError: ‘NoneType’ object has no attribute ‘append’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘NoneType’ object has no attribute ‘append’” tells us that the NoneType object does not have the attribute append(). The append() method belongs to the List data type, and appends elements to the end of a list.

A NoneType object indicates no value:

obj = None
print(type(obj))
<class 'NoneType'>

Let’s look at the syntax of the append method:

list.append(element)

Parameters:

  • element: Required. An element of any type to append.

The append method does not return a value, in other words, it returns None. If we assign the result of the append() method to a variable, the variable will be a NoneType object.

Example

Let’s look at an example where we have a list of strings, and we want to append another string to the list. First, we will define the list:

# List of planets

planets = ["Jupiter", "Mars", "Neptune", "Saturn"]

planets = planets.append("Mercury")

print(planets)

planets = planets.append("Venus")

print(f'Updated list of planets: {planets}')

Let’s run the code to see what happens:

None
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
      5 planets = planets.append("Mercury")
      6 
----≻ 7 planets = planets.append("Venus")
      8 
      9 print(f'Updated list of planets: {planets}')

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

The error occurs because the first call to append returns a None value assigned to the planets variable. Then, we tried to call append() on the planets variable, which is no longer a list but a None value. The append() method updates an existing list; it does not create a new list.

Solution

We need to remove the assignment operation when calling the append() method to solve this error. Let’s look at the revised code:

# List of planets

planets = ["Jupiter", "Mars", "Neptune", "Saturn"]

planets.append("Mercury")

planets.append("Venus")

print(f'Updated list of planets: {planets}')

Let’s run the code to see the result:

Updated list of planets: ['Jupiter', 'Mars', 'Neptune', 'Saturn', 'Mercury', 'Venus']

We update the list of planets by calling the append() method twice. The updated list contains the two new values.

Summary

Congratulations on reading to the end of this tutorial! The error AttributeError: ‘NoneType’ object has no attribute ‘append’ occurs when you call the append() method on a NoneType object. This error commonly occurs if you call the append method and then assign the result to the same variable name as the original list. The append() method returns None, so you will replace the list with a None value by doing this.

For further reading on AttributeErrors, go to the article: How to Solve Python AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’.

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!