The Python append()
method returns a None value. This is because appending an item to a list updates an existing list. It does not create a new one.
If you try to assign the result of the append() method to a variable, you encounter a “TypeError: ‘NoneType’ object has no attribute ‘append’” error.
In this guide, we talk about what this error means, why it is raised, and how you can solve it, with reference to an example.
TypeError: ‘NoneType’ object has no attribute ‘append’
In Python, it is a convention that methods that change sequences return None. The reason for this is because returning a new copy of the list would be suboptimal from a performance perspective when the existing list can just be changed.
Because append()
does not create a new list, it is clear that the method will mutate an existing list. This prevents you from adding an item to an existing list by accident.
A common mistake coders make is to assign the result of the append()
method to a new list. This does not work because append()
changes an existing list. append()
does not generate a new list to which you can assign to a variable.
An Example Scenario
Next, we build a program that lets a librarian add a book to a list of records. This list of records contains information about the author of a book and how many copies are available.
Let’s start by defining a list of books:
books = [ { "title": "The Great Gatsby", "available": 3 } ]
The books list contains one dictionary. A dictionary stores information about a specific book. We add one record to this list of books:
books = books.append( { "title": "Twilight", "available": 2 } )
Our “books” list now contains two records. Next, we ask the user for information about a book they want to add to the list:
title = input("Enter the title of the book: ") available = input("Enter how many copies of the book are available: ")
Now that we have this information, we can proceed to add a record to our list of books. We can do this using the append() method:
books = books.append( { "title": title, "available": int(available) } )
We’ve added a new dictionary to the “books” list. We have converted the value of “available” to an integer in our dictionary. We assign the result of the append()
method to the “books” variable. Finally, we print the new list of books to the console:
print(books)
Let’s run our code and see what happens:
Enter the title of the book: Pride and Prejudice Enter how many copies of the book are available: 5 Traceback (most recent call last): File "main.py", line 12, in <module> books = books.append( AttributeError: 'NoneType' object has no attribute 'append'
Our code successfully asks us to enter information about a book. When our code tries to add the book to our list of books, an error is returned.
The Solution
Our code returns an error because we’ve assigned the result of an append()
method to a variable. Take a look at the code that adds Twilight to our list of books:
books = books.append( { "title": "Twilight", "available": 2 } )
This code changes the value of “books” to the value returned by the append()
method. append()
returns a None value. This means that “books” becomes equal to None.
When we try to append the book a user has written about in the console to the “books” list, our code returns an error. “books” is equal to None and you cannot add a value to a None value.
To solve this error, we have to remove the assignment operator from everywhere that we use the append()
method:
books.append( { "title": "Twilight", "available": 2 } ) … books.append( { "title": title, "available": int(available) } )
We’ve removed the “books = ” statement from each of these lines of code. When we use the append()
method, a dictionary is added to books. We don’t assign the value of “books” to the value that append()
returns.
Let’s run our code again:
Enter the title of the book: Pride and Prejudice Enter how many copies of the book are available: 5 [{'title': 'The Great Gatsby', 'available': 3}, {'title': 'Twilight', 'available': 2}, {'title': 'Pride and Prejudice', 'available': 5}]
Our code successfully adds a dictionary entry for the book Pride and Prejudice to our list of books.
Conclusion
The “TypeError: ‘NoneType’ object has no attribute ‘append’” error is returned when you use the assignment operator with the append()
method.
To solve this error, make sure you do not try to assign the result of the append()
method to a list. The append()
method adds an item to an existing list. The method returns None, not a copy of an existing list.
Now you’re ready to solve this common Python problem like a professional!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.