# Numpy Insert In Python With Examples

Hello geeks and welcome to today’s article, we will discuss NumPy insert(). Along with it, we will also cover its syntax and parameters. For a better understanding, we will also look at a couple of examples. NumPy is a powerful mathematical library of python which provides us with a function insert.

NumPy insert() helps us by allowing us to insert values in a given axis before the given index number. To execute this operation, there are several parameters that we need to take care of. One such thing is the axis; if not defined, then the input array is flattened first. We will read more about parameters as we move ahead in the article.

Contents

## Syntax Of Numpy Insert()

Given below is the syntax of NumPy.insert()

``numpy.insert`(arr, obj, values, axis=None)`

This is the standard syntax that has a total of 4 parameters. Each parameter is important in its own way. Up next, we will cover each parameter in detail.

## Parameter Of Numpy Insert()

arr: array_like
This represents the input array on which the operation has to be carried.

obj: int, slice
This represents the index before which the new value needs to insert.

values: array_like
This parameter represents the value to be inserted in the array. The value to be inserted must have a data type similar to that of the array, or else it undergoes a conversion.

Axis: int
It is an optional parameter, and as stated above, if not defined, the input array is flattened. This means that the input array is converted to a one 1 dimensional array.

## Return

OUT: ndarray
It returns an input array with the new value inserted.

## Examples Of Numpy Insert()

Now let us look at a couple of examples that will help us in the understanding of Numpy insert()

```import numpy as ppool
a=ppool.array([[2,4],[6,8],[10,12]])
print(a)
print(ppool.insert(a,1,5))
```
```#output
[[ 2  4]
[ 6  8]
[10 12]]
#without axis
[ 2  5  4  6  8 10 12]
```

### Explanation

Here in the above example, we have imported NumPy first. In the next step, we have defined the array that can be termed as the input array. On which all the operations will be performed. Then following the proper syntax we have written: “ppool.insert(a,1,5)“.

Here the notation “a” represents the array name, “1” represents the index at which the value needs to be inserted. “5” represents the value to be inserted, and output justifies it. One more important thing to take note of is that here we have not defined any axis. As a result of which we get a flattened array ( 1 dimension) as an output.

## Now let us look at another example

```import numpy as ppool
a=ppool.array([[2,4],[6,8],[10,12]])
print(a)
print(ppool.insert(a,1,5,axis=1))
```
```#output
[[ 2  4]
[ 6  8]
[10 12]]
#with axis
[[ 2  5  4]
[ 6  5  8]
[10  5 12]]
```

The above example is the same as the one we dealt with earlier. With the only difference of axis and if you compare the two outputs, change is quite evident. Here we don’t get any 1-dimensional matrix. Rather at every 1st location of the matrix, the value is inserted.

Now let’s look at one last example in which we will insert elements using a sequence.

```import numpy as ppool
a=ppool.array([[2,4],[6,8],[10,12]])
print(ppool.insert(a,1,[5,2,3],axis=1))
```
```#output
[[ 2  5  4]
[ 6  2  8]
[10  3 12]]
```

Here we can see in the same example we have used a sequence. Instead of adding a similar value, we have added different values at index number 1 of sub-arrays.

## How to use Numpy Insert on 2D array?

You can specify the axis and index along which you have insert items in 2D Numpy Array. The 2nd parameter in the Numpy Insert function refers to the index and the 4th parameter refers to the axis of the 2D array.

Code:

```import numpy as np

a = np.zeros((2, 3))
print(a)
a = np.insert(a, 1, np.array((2, 4, 1)), 0)
print(a)
```

Output:

``````[[0. 0. 0.]
[0. 0. 0.]]
[[0. 0. 0.]
[2. 4. 1.]
[0. 0. 0.]]`````` 