*If you attempt to call the *append()* method on a NumPy array, you will raise the error AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’. To solve this error, use the numpy.append() method.*

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

## Table of contents

## AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’

### What is an AttributeError?

*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 “‘numpy.ndarray’ object has no attribute ‘append'” tells us that the numpy array object we are handling does not have the append attribute. The append() method belongs to the ordinary Python list:

```
lst = [1, 2, 3]
lst.append(4)
print(lst)
```

`[1, 2, 3, 4]`

## Example

Let’s look at an example where we define a numpy array of integers and append another value to the array. First, let’s create the NumPy array:

```
import numpy as np
# Create NumPy array
arr = np.array([4, 4, 1, 6, 7, 22, 23, 10, 15, 16])
```

Note that we use the alias for the numpy library, np.

Next, we will try to append another value to the array:

```
# Get maximum value of array
arr.append(2)
```

Let’s run the code to see what happens:

```
----------------------------------------------------------------------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
----â‰» 1 arr.append(2)
AttributeError: 'numpy.ndarray' object has no attribute 'append'
```

The code throws the error because the append() method does not belong to the NumPy array object. The append() method only works on a normal Python list.

### Solution: Use the numpy.append() Method

To solve the error, we can use the numpy.append() method. The syntax for using for numpy.append() is

`numpy.append(arr, values, axis=None)`

**Parameters**

- arr: an array of values to append to
- values: values to append to array
*arr* - axis: Axis to append values along. If not specified, method flattens both arr and values before appending.

**Returns**

- A copy of arr with values appended to axis. Note that append does not occur in-place. The return value of numpy.append() is an array.

Let’s look at the revised example with the numpy.append() method

```
import numpy as np
# Create NumPy array
arr = np.array([4, 4, 1, 6, 7, 22, 23, 10, 15, 16])
# Append value to end of NumPy array
arr = np.append(arr, 2)
print(f'Updated array is {arr}')
```

Let’s run the code to see the result:

`Updated array is [ 4 4 1 6 7 22 23 10 15 16 2]`

The number 2 is now at the end of the numpy array.

### Using numpy.concatenate()

If you want to append one NumPy array to another NumPy array, it is best to use the numpy.concatenate() method. With this method, you can concatenate as many arrays as you want, provided the arrays have the same shape. Let’s look at an example:

```
import numpy as np
# Create Two NumPy array
x = np.array([2, 3, 21, 1, 8, 9, 21, 4, 18, 6, 21])
y = np.array([4, 5, 10, 3, 20, 12, 1])
# Concatenate the two arrays
z = np.concatenate((x,y))
d
print(f'Concatenation result is {z}')
```

When we pass the arrays to the concatenate method, they must be in a tuple; you need to put the values in parentheses (). If you do not pass a tuple, the Python interpreter will raise the error: TypeError: only integer scalar arrays can be converted to a scalar index.

Let’s run the code to get the result:

`Concatenation result is [ 2 3 21 1 8 9 21 4 18 6 21 4 5 10 3 20 12 1]`

## Summary

Congratulations on reading to the end of this tutorial! The error AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ occurs when you try to call the append() method on a numpy array. You must use the numpy.append() method instead. If you want to append an array to another array, you can use the numpy.concatenate() method.

For further reading on AttributeErrors, go to the articles:

- How to Solve AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’.
- How to Solve Python AttributeError: ‘NoneType’ 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!

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!