# Add elements to end of NumPy array

In this post, we are going to understand how to add elements at the end of NumPy array.NumPy array. The numpy library insert(), append(), concatenate() and hstack() is used to add single or multiple elements at once to the end of a numpy array. To run all programs make sure numpy is installed on the local machine.

### 1. np.insert() to Add elements to end of NumPy array

The Numpy library insert() function adds values in the numpy array before the given indices along with the axis. So it takes an index and value that is inserted into the given index. We can add single or multiple elements at end of the existing array using np.insert() function. We will pass the length of the array to add an element at end of an existing array.

In this example, we have added an array of two elements at the end of an existing 1D array as well as added an array at end of the existing 2D numpy array by using np.insert() method along with the axis.

```import numpy as np
arr = np.array([10,12,23,24,15,16])

print('original array:\n',arr)

result_arr = np.insert(arr,len(arr),[30,33])

print('Updated array:\n',result_arr)

arr2= np.array([[10,12,23],[24,15,16]])

print('original 2D array:\n',arr2)

res_Array= np.insert(arr2,len(arr2),[4,8,12],axis=0)
print('updated  2D array:\n',res_Array)

```

Output

```original array:
[10 12 23 24 15 16]
Updated array:
[10 12 23 24 15 16 30 33]

original 2D array:
[[10 12 23]
[24 15 16]]
Updated 2D array:
[[10 12 23]
[24 15 16]
[ 4  8 12]]
```

### 2. np.append() to add element to end of NumPy array

The Numpy appends() function adds an element in a NumPy array at the end. This method does not modify the original array else returns a copy of the array after adding the passed element or array. In this program, We have appended an element and an array at end of an existing array.

```import numpy as np

myarr = np.array([12,14,700,60,50])
numarr = np.array([3,6,9])

#append an element at end of array

resarr = np.append(myarr, 90)
print('append an element in Numpy array: ', resarr)

#append array at end of array

resarr = np.append(myarr,numarr)
print('\n appended an Array in Numpy array: ', resarr)

```

Output

```append an element in Numpy  array:  [ 12  14 700  60  50  90]

appended an Array in Numpy array :  [ 12  14 700  60  50   3   6   9]
```

### 3.np.concatenate() to add element to end of Numpy array

The NumPy array concatenate() function concate two numpy array of same shape either row-wise or column-wise.This function by default concatenate arrays row-wise (axis=0).

#### Syntax

```numpy.concatenate((a1, a2, ...an), axis=0, out=None, dtype=None)
```

#### Parameters

• a1..an: The sequence of array_like objects. The sequence must be of the same shape.
• axis: The axis along which array is joined.It is an optional parameter.
• axis=none: The input array is flattened before use.
• axis =1: The array is joined column-wise.
• axis = 0: The array is joined row-wise.It is the default value of the axis.
• Out: optional The output ndarray if the provided output is placed in this array.The shape must be correct.
• dtype : optional if provided the output array of this type or str.

### 3.1 Python program to add elements to end 1D Numpy array

In this python program example, we have to add or concatenate an array at end of the existing 1D numpy array. This is how we use numpy concatenate function to add an array at end of an existing array.

```#Python program to  Add element to end of Numpy array
import numpy as np

nparr1 = np.array([13, 14, 15, 18, 20])

nparr2 = np.array([22, 32, 33, 34, 36])

joined_nparr = np.concatenate( (nparr1, nparr2) )
print(joined_nparr)

```

Output

```[13 14 15 18 20 22 32 33 34 36]
```

### 3.2 Python program to add elements to end 2D Numpy array

In this python program example, we have to add or concatenate an array at end of the existing 2D numpy array along with the axis row-wise and column-wise.

```import numpy as np

nparr1 = np.array([[13, 14, 15],
[26, 12, 23],
[24, 25, 30]])

nparr2 = np.array([ [11, 54, 9],
[3, 6, 7],
[4, 8, 62]])
# Concatenate 2D np Arrays column wise
joined_nparr = np.concatenate( (nparr1, nparr2), axis=1 )

# Concatenate 2D np Arrays row-wise
join_nparr_rowwise = np.concatenate( (nparr1, nparr2), axis=0 )

print('concatenate column-wise:\n',joined_nparr)
print('\n concatenate row-wise:',join_nparr_rowwise)
```

Output

```concatenate column-wise:
[[13 14 15 11 54  9]
[26 12 23  3  6  7]
[24 25 30  4  8 62]]

concatenate row-wise:
[[13 14 15]
[26 12 23]
[24 25 30]
[11 54  9]
[ 3  6  7]
[ 4  8 62]]
```

### 4. np.hstack() to Add element to end of NumPy array

The numpy library hstack() function is a stacked sequence of input arrays horizontally column-wise.In this python program example, we have added multiple elements to end the existing array by simply passing a numpy array. We can add single or multiple elements at once at end of a numpy array by passing a numpy array of single or multiple elements using np. hstack() function

```import numpy as np

nparr = np.array([13, 14, 15, 18, 20])

print('original array:\n',nparr)
resultArr = np.hstack((nparr, [3,6,9,12]))

print('updated array:\n',resultArr)

```

Output

```original array:
[13 14 15 18 20]
updated array:
[13 14 15 18 20  3  6  9 12]
```