In this article we will discuss how to append elements at the end on a Numpy Array in python.

## numpy.append()

Python’s Numpy module provides a function to append elements to the end of a Numpy Array.

Arguments:
arr : An array like object or a numpy array.
values: An array like instance of values to be appended at the end of above mention array.
axis : It’s optional and Values can be 0 & 1.

It doesn’t modify the original array in parameter arr. It creates a copy of this array and appends the elements from values param to the end of this new copied array. So, basically it returns a copy of numpy array provided with values appended to it.

If axis parameter is
Not Provided : Then both the arrays arr & values can be of different shapes and will bbe flattened before use.
0 : values will be appended to arr at axis 0 i.e. new rows will be added
1 : values will be appended to arr at axis 1 i.e. new columns will be added

Let’s understand by examples :

## Append elements at the end of numpy array (without axis argument)

Let’s create a Numpy array i.e.

Append a single element to the Numpy array

Contents of the new Numpy Array returned :

Now let’s see how append multiple elements to a Numpy array.

Append elements from a list to the Numpy array

Contents of the new Numpy Array returned :

## Append elements in Numpy array with different shapes

Let’s create a 2D numpy array i.e.

Now append 1D list to this 2D Numpy array.

As axis parameter is not provided in call to append(), so both the arrays will be flattened first and then values will appended. Therefore, contents of the new flattened Numpy Array returned are,

## Append elements in Numpy array on Axis 0 | Append Rows

If we provide axis parameter in append() call then both the arrays should be of same shape. Let’s create two 2D numpy arrays,

Now let’s append the rows from a numpy array to the end of another numpy array by passing axis as 0 i.e.

Contents of 2D array matrixArr2 will be appended to the contents of matrixArr1  as rows in new array. Contents of the returned array are,

## Append elements in Numpy array on Axis 1 | Append Columns

In the above example if instead of passing axis as 0 we pass axis=1 then contents of 2D array matrixArr2 will be appended to the contents of matrixArr1  as columns in new array i.e.

Contents of the new Numpy Array returned are,

## Error while append elements in Numpy array of different shapes

If you are providing axis parameter in numpy.append() then both the arrays should be of same shape otherwise it will raise Error. For example,

Let’s try to append a 1D array to 2D array with axis = 1 i.e.

It will give following error,

Complete example is as follows,

Output:

### Design Patterns Resources

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.