[Solved] AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’

If you use the regular Python list append() method to add an element to the end of NumPy array, you will encounter AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’.

In this tutorial, we will look at what exactly is AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ and how to resolve this issue with examples.

What is AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’?

In Python, it is common to use the append() method to add an element to the end of the array like we do in the list.

Similarly, if we attempt to append one or more elements to the end of the NumPy array using the regular list append() method, we will encounter AttributeError.

The NumPy arrays are different compared to the regular Python lists. Let us take a simple example to demonstrate the issue.

# import numpy library
import numpy as np

# define NumPy array
cars = np.array(["Toyota","Volkswagen","Tesla","Ford"])

# append the new car into numpy array
cars.append("Chevrolet")

# print the appended cars
print(cars)

Output

Traceback (most recent call last):
  File "c:\Personal\IJS\Code\main.py", line 8, in <module>
    cars.append("Chevrolet")
AttributeError: 'numpy.ndarray' object has no attribute 'append'

If you look at the output error message, it’s self-explanatory that the NumPy array has a type numpy.ndarray which does not have an append() attribute.

How to fix AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’?

Now that we have understood that we cannot use the append() method to add an element to the numpy.ndarray like we do it on the regular list, let us check the other options that we have to solve the issue. 

The NumPy has its own built-in method called numpy.append(), which can be used to append or add an item to the NumPy array.

Syntax of numpy.append()

numpy.append(arrvaluesaxis=None)

Parameters of numpy.append()

  • arr – The NumPy array to which the values need to be appended. Values are appended to a copy of this array.
  • values – These values are appended to a copy of arr. It must be in the correct shape (the same shape as arr, excluding axis). If the axis is not specified, values can be any shape and will be flattened before use.
  • axis (optional) – The axis along which values are appended. If the axis is not given, both arr and values are flattened before use.

Return Value of numpy.append()

The numpy.append() method returns a new array after appending the specified items to the end of an array. The output array is flattened if the axis is not provided or None.

Let us use the np.append() and fix the AttributeError issue that occurred in our example.

Example – Append the items to NumPy array using numpy.append() method

# import numpy library
import numpy as np

# define NumPy array
cars = np.array(["Toyota", "Volkswagen", "Tesla", "Ford"])

# append the new cars into numpy array
cars = np.append(cars, ["Chevrolet", "Jeep"])

# print the appended cars
print(cars)

Output

['Toyota' 'Volkswagen' 'Tesla' 'Ford' 'Chevrolet' 'Jeep']

We use the term np to refer to the NumPy library. It is an alias to the NumPy itself, as we have defined this while importing the NumPy library.

The above examples execute successfully and append the items into the end of the numpy array.

Conclusion

The AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ occurs if you attempt to append an element into NumPy array using the regular append() method like we do in the list. The numpy.ndarray does not have an append method, and hence it throws AttributeError.

We can resolve this error by using the numpy.append() method provided by the NumPy library. The numpy.append() method returns a copy of an array with values appended to the specified axis. If the axis is not specified, it will return a flattened array.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Python String Rjust().Png

Python String rjust()

Table of Contents Hide rjust() Syntaxrjust() Parametersrjust() Return valueExample 1 : Python program to right justify a stringExample 2: rjust() Method With * fillcharExample 3: Returns original string if the…
View Post
Python List Insert()

Python List insert()

Table of Contents Hide Syntax of List insert() insert() ParametersReturn Value from List insert()Example 1: Inserting an Element to the ListExample 2: Inserting an element at the start and end of…
View Post
Python Enumerate()

Python enumerate()

Table of Contents Hide enumerate() Syntax enumerate() Parametersenumerate() Return ValueExample 1: How enumerate() method works in Python?Example 2: Using Enumerate object in loops The enumerate() in Python is a built-in function that adds…
View Post
Python String Split()

Python String split()

Table of Contents Hide split() Syntaxsplit() Parameterssplit() Return ValueExample 1: How split() works in Python?Example 2: How split() works when maxsplit is specified? The Python String split() method is a…
View Post