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.

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

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.

```# create a Numpy array from a list
arr = numpy.array([1, 2, 3, 4, 5, 6, 7])
```

Append a single element to the Numpy array
```# Append a single element at the end of Numpy Array
newArr = numpy.append(arr, 88)```

Contents of the new Numpy Array returned :
`[ 1  2  3  4  5  6  7 88]`

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

Append elements from a list to the Numpy array

```# Append multiple elements from a list to the end of a Numpy Array
newArr = numpy.append(arr, [88,99,100])
```

Contents of the new Numpy Array returned :
`[  1   2   3   4   5   6   7  88  99 100]`

## Append elements in Numpy array with different shapes

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

```# Create a 2D Numpy Array like Matrix
matrixArr = numpy.array( [ [1, 2, 3],
[ 4, 5, 6] ])
```

Now append 1D list to this 2D Numpy array.
```# Add elements in List to 2D Numpy array by flattening
newArr = numpy.append(matrixArr, [22, 23, 24])
```

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,
`[ 1  2  3  4  5  6 22 23 24]`

## 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,

```# Create two 2D Numpy Array like Matrix
matrixArr1 = numpy.array([[1, 2, 3],
[4, 5, 6]])

matrixArr2 = numpy.array([[70, 80, 90],
[61, 62, 63]])
```

Now let’s append the rows from a numpy array to the end of another numpy array by passing axis as 0 i.e.
`newArr = numpy.append(matrixArr1, matrixArr2 , axis=1)`

Contents of 2D array matrixArr2 will be appended to the contents of matrixArr1  as rows in new array. Contents of the returned array are,
```[[1 2 3]
[4 5 6]
[1 2 3]
[4 5 6]]```

## 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.

`newArr = numpy.append(matrixArr1, matrixArr2 , axis=1)`

Contents of the new Numpy Array returned are,
```[[ 1  2  3 70 80 90]
[ 4  5  6 61 62 63]]
```

## 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.

`arr5 = numpy.append(matrixArr, [22, 23, 24], axis=1 )`

It will give following error,
```ValueError: all the input arrays must have same number of dimensions
```

Complete example is as follows,
```import numpy as numpy

def main():

# create a Numpy array from a list
arr = numpy.array([1, 2, 3, 4, 5, 6, 7])

print('Contents of the Numpy Array arr : ', arr)

print('*** Append elements at the end of a numpy array ***')

# Append a single element at the end of Numpy Array
newArr = numpy.append(arr, 88)

print('Contents of the new Array : ', newArr)

# Append multiple elements from a list to the end of a Numpy Array
newArr = numpy.append(arr, [88,99,100])

print('Contents of the new Array : ', newArr)

print('*** Append elements in Numpy array with different shapes ***')

# Create a 2D Numpy Array like Matrix
matrixArr = numpy.array( [ [1, 2, 3],
[ 4, 5, 6] ])

print('Contents of 2D Numpy Array : ')
print(matrixArr)

# Add elements in List to 2D Numpy array by flattening
newArr = numpy.append(matrixArr, [22, 23, 24])

print('Contents of the new Array : ', newArr)

print('*** Append elements in Numpy array on axis 0 | Append Rows***')

# Create two 2D Numpy Array like Matrix
matrixArr1 = numpy.array([[1, 2, 3],
[4, 5, 6]])

matrixArr2 = numpy.array([[70, 80, 90],
[61, 62, 63]])

newArr = numpy.append(matrixArr1, matrixArr1 , axis=0)

print('Contents of the new Array : ', newArr, sep='\n')

print('*** Append elements in Numpy array on axis 1 | Append Columns ***')

newArr = numpy.append(matrixArr1, matrixArr2 , axis=1)

print('Contents of the new Array : ', newArr, sep='\n')

#arr5 = numpy.append(matrixArr, [22, 23, 24], axis=1 )

#print(arr5)

if __name__ == '__main__':
main()

```

Output:
```Contents of the Numpy Array arr :  [1 2 3 4 5 6 7]
*** Append elements at the end of a numpy array ***
Contents of the new Array :  [ 1  2  3  4  5  6  7 88]
Contents of the new Array :  [  1   2   3   4   5   6   7  88  99 100]
*** Append elements in Numpy array with different shapes ***
Contents of 2D Numpy Array :
[[1 2 3]
[4 5 6]]
Contents of the new Array :  [ 1  2  3  4  5  6 22 23 24]
*** Append elements in Numpy array on axis 0 | Append Rows***
Contents of the new Array :
[[1 2 3]
[4 5 6]
[1 2 3]
[4 5 6]]
*** Append elements in Numpy array on axis 1 | Append Columns ***
Contents of the new Array :
[[ 1  2  3 70 80 90]
[ 4  5  6 61 62 63]]```