# NumPy: numpy.append() function

## numpy.append() function

The numpy.append() function is used to append values to the end of an existing array. It can append values to the flattened version of an array, or to a specified axis of a multi-dimensional array.

Here are some applications of numpy.append() function:

• Concatenate two or more arrays together to form a larger array.
• Add a new row or column to an existing array.
• Create a new array by appending an element to an existing array.
• Generate a sequence of numbers with a specific pattern by using numpy.append() in a loop.

Syntax:

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

Parameters:

Name Description Required /
Optional
arr Values are appended to a copy of this array. Required
values These values are appended to a copy of arr. It must be of the correct shape (the same shape as arr, excluding axis). If axis is not specified, values can be any shape and will be flattened before use. Required
axis The axis along which values are appended. If axis is not given, both arr and values are flattened before use. Optional

Return value:

append : ndarray - A copy of arr with values appended to axis. Note that append does not occur in-place: a new array is allocated and filled. If axis is None, out is a flattened array.

Example: Appending arrays in NumPy using numpy.append()

``````>>> import numpy as np
>>> np.append ([0, 1, 2], [[3, 4, 5], [6, 7, 8]])
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
``````

In the above code, the np.append() function is used to append arrays in NumPy. The first argument passed to the function is a one-dimensional NumPy array and the second argument is a two-dimensional NumPy array.

Pictorial Presentation:

Example: Appending a 2D array to another 2D array along the vertical axis

``````>>> import numpy as np
>>> np.append([[0, 1, 2], [3, 4, 5]],[[6, 7, 8]], axis=0)
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
``````

In the above code, we have two 2D arrays with the shape (2,3) and (1,3) respectively. We use the np.append() function to append the second array vertically to the first array.

Pictorial Presentation:

Python - NumPy Code Editor:

Previous: insert()
Next: resize()

﻿