# numpy.insert() – The NumPy insert Python Function

## Introduction to NumPy insert

The numpy.insert() function inserts values along the mentioned axis before the given indices.

Syntax:

`numpy.insert(array, object, values, axis = None)`

Parameters:

• array : [array_like] Input array.
• object : [int, array of ints] Sub-array with the index or indices before which values are inserted
• values : [array_like] values to be added in the arr. Values should be shaped so that arr[…, obj,…] = values. If the type of values is different from that of arr, values are convert to the type of arr
• axis: the Axis along which we want to insert the values. By default, it object is applies to a flattened array

Return: An copy of the array with values being insert as per the mention object along a given axis.

## Code Examples of NumPy insert

Example 01: Working with arrays

``````# welcome to softhunt.net
# Python Program illustrating
# numpy.insert()

import numpy as np

#Working on 1D
arr = np.arange(5)
print("1D arr : \n", arr)
print("Shape : ", arr.shape)

# value = 9
# index = 1
# Insertion before first index
a = np.insert(arr, 1, 9)
print("\nArray after insertion : ", a)
print("Shape : ", a.shape)

# Working on 2D array
arr = np.arange(12).reshape(3, 4)
print("\n\n2D arr : \n", arr)
print("Shape : ", arr.shape)

a = np.insert(arr, 1, 9, axis = 1)
print("\nArray after insertion : \n", a)
print("Shape : ", a.shape)``````

Output:

```1D arr :
[0 1 2 3 4]
Shape :  (5,)

Array after insertion :  [0 9 1 2 3 4]
Shape :  (6,)

2D arr :
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]
Shape :  (3, 4)

Array after insertion :
[[ 0  9  1  2  3]
[ 4  9  5  6  7]
[ 8  9  9 10 11]]
Shape :  (3, 5)```

Example 02: Working with Scalars

``````# welcome to softhunt.net
# Python Program illustrating
# numpy.insert()

import numpy as np

# Working on 2D array
arr = np.arange(12).reshape(3, 4)
print("2D arr : \n", arr)
print("Shape : ", arr.shape)

# Working with Scalars
a = np.insert(arr, [1], [[6],[9],], axis = 0)
print("\nArray after insertion : \n", a)
print("Shape : ", a.shape)

# Working with Scalars
a = np.insert(arr, [1], [[8],[7],[9]], axis = 1)
print("\nArray after insertion : \n", a)
print("Shape : ", a.shape)``````

Output:

```2D arr :
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]
Shape :  (3, 4)

Array after insertion :
[[ 0  1  2  3]
[ 6  6  6  6]
[ 9  9  9  9]
[ 4  5  6  7]
[ 8  9 10 11]]
Shape :  (5, 4)

Array after insertion :
[[ 0  8  1  2  3]
[ 4  7  5  6  7]
[ 8  9  9 10 11]]
Shape :  (3, 5)```

Example 03: Insertion at different points

``````# welcome to softhunt.net
# Python Program illustrating
# numpy.insert()

import numpy as np

#Working on 1D
arr = np.arange(6).reshape(2, 3)
print("1D arr : \n", arr)
print("Shape : ", arr.shape)

# value = 9
# index = 1
# Insertion before first index
a = np.insert(arr, (2, 4), 9)
print("\nInsertion at two points : ", a)
print("Shape : ", a.shape)

# Working on 2D array
arr = np.arange(12).reshape(3, 4)
print("\n\n2D arr : \n", arr)
print("Shape : ", arr.shape)
a = np.insert(arr, (0, 3), 66, axis = 1)
print("\nInsertion at two points : \n", a)
print("Shape : ", a.shape)
``````

Output:

```1D arr :
[[0 1 2]
[3 4 5]]
Shape :  (2, 3)

Insertion at two points :  [0 1 9 2 3 9 4 5]
Shape :  (8,)

2D arr :
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]
Shape :  (3, 4)

Insertion at two points :
[[66  0  1  2 66  3]
[66  4  5  6 66  7]
[66  8  9 10 66 11]]
Shape :  (3, 6)```

## FAQs

### How does NumPy insert work?

This function inserts values in the input array along the given axis and before the given index. If the type of values is convert to be insert, it is different from the input array. Insertion is not done in place and the function returns a new array. Also, if the axis is not mention, the input array is flatten.

### What is the axis in NumPy?

The Numpy Axis is the direction in which the iteration begins. Every operation in NumPy has its own iteration process that it follows. There are two different sorts of iteration processes: column order and Fortran order. The column axis is help by column order, while the row axis is help by Fortran order.

## Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

1. numpy.delete() – The NumPy delete Python Function