In regular Python, you can use the append()
method to add an item to the end of a list. You cannot use this method in NumPy. If you try to use the Python append()
method to add an item to the end of a NumPy array, you will see the AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’
error.
This guide discusses in detail the cause of and the solution to this NumPy error. We will refer to an example to illustrate how to fix this error. Let’s get started.
AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’
The AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’
error is caused by using the append()
method to add an item to a NumPy array. You should instead use the numpy.append()
method if you want to add an item to a list.
The numpy.append()
method was specifically written for the NumPy library. NumPy arrays are different to regular Python arrays so it is reasonable that NumPy has its own method to add an item to an array.
The NumPy append()
method uses this syntax:
numpy.append(list_to_add_item, item_to_add)
The two parameters we will focus on are:
- list_to_add_item: The list to which you want to add an item.
- item_to_add: The item you want to add to the list you specify.
The numpy.append()
method returns a new array which contains your specified item at the end, based on the “list_to_add_item” array. Note that you do not put append()
after the list to which you want to add an item, like you would in regular Python.
Let’s go through an example of this error.
An Example Situation
We are building an application that tracks the performance grades a product has received after quality assurance at a factory. The products are scored on a scale of 50 and all products must achieve a score of at least 40 to go out into the world.
We are building the part of the application that adds new scores to an array which stores the scores a product has received for the last day. To build this program, we can use the append()
method:
import numpy as np scores = np.array([49, 48, 49, 47, 42, 48, 46, 50]) to_add = 49 scores.append(to_add) print(scores)
Our program adds the score 39 to our list of scores. In a real-world situation, we may read these scores from a file, but to keep our example simple we have declared an array in our program. Our code prints a list of all the scores to the Python console after the new score is added to our array of scores.
Let’s run our code and see what happens:
Traceback (most recent call last): File "test.py", line 6, in <module> scores.append(to_add) AttributeError: 'numpy.ndarray' object has no attribute 'append'
Our code returns an error.
The Solution
We are trying to use the regular Python append()
method to add an item to our NumPy array, instead of the custom-build numpy.append()
method.
To solve this error, we need to use the syntax for the numpy.append()
method:
import numpy as np scores = np.array([49, 48, 49, 47, 42, 48, 46, 50]) scores = np.append(scores, 49) print(scores)
We use the np term to refer to the NumPy library. This works because we defined the numpy library as np in our import statement. We pass the list to which we want to add an item as our first argument; the new score to add to our array is our second argument.
We have to assign the result of our np.append()
operation to a new value. This is because np.append()
does not modify an existing array. Instead, the method creates a new array with your new value added.
Let’s run our program and see what happens:
[49 48 49 47 42 48 46 50 49]
The number 49 has been successfully added to the end of our list.
Conclusion
The AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’
error indicates you are using the regular Python append()
method to add an item to a NumPy array. Instead, you should use the numpy.append()
method, which uses the syntax: numpy.append(list, item_to_add). This method creates a new list with the specified item added at the end.
Do you want to learn more about coding in NumPy? Check out our How to Learn NumPy guide. This guide contains top tips on how to build your knowledge of NumPy, alongside a list of learning resources suitable for beginner and intermediate developers.
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.