NumPy insert()

The insert() method adds the values along the given axis at specified indices.

Example

import numpy as np

# create an array
numbers = np.array([0, 1, 3, 4])

# insert 4 at index 2
array2 = np.insert(numbers, 2, 4)

print(array2)

# Output : [0 1 4 3 4]

Here, the insert() method inserted a new item 4 to the index 2, and shifted the previous item to the next index.


insert() Syntax

The syntax of insert() is:

numpy.insert(array, obj, values, axis)

insert() Arguments

The insert() method takes four arguments:

  • array - original array
  • obj - indices at which values are inserted
  • values - the array to be inserted at the given position
  • axis(optional) - the axis along which the values are inserted

insert() Return Value

The insert() method returns an array with values inserted.


Example 1: Insert an Array at Given Index

import numpy as np

array1 = np.array([0, 1, 2, 3])
array2 = np.array([4, 5, 6, 7])

# insert values from array2 to array1 before index 2 array3 = np.insert(array1, 2, array2)
print(array3)

Output

[0 1 4 5 6 7 2 3]

Example 2: Insert an Array Element at Different Indices

We can insert different values at different indices.

import numpy as np

array1 = np.array([0, 1, 2, 3])
array2 = np.array([4, 5, 6, 7])

# insert values from array2 to array1 before given indices array3 = np.insert(array1, [2, 3, 2, 1], array2)
print(array3)

Output

[0 7 1 4 6 2 5 3]

Note: When passing a sequence as obj, the size of the sequence should match the size of the values. For example, when inserting 4 values, 4 indices should be provided.


Example 3: Insert into a 2-D Array Before Given Index

Similar to a 1-D array, values can be inserted to a 2-D array at any index along any axis.

import numpy as np

array1 = np.array([[0, 1], [2, 3]])
array2 = np.array([4, 5])

# insert values to an array before index 2 array3 = np.insert(array1, 2, array2)
print('\nInsert to 2D array at index 2\n', array3)
# insert values to an array before index 2 along axis 0 array4 = np.insert(array1, 2, array2, 0)
print('\nInsert to 2D array at row 2\n', array4)
# insert values to an array before index 2 along axis 1 array5 = np.insert(array1, 2, array2, 1)
print('\nInsert to 2D array at column 2\n', array5)

Output

Insert to 2D array at index 2
[0 1 4 5 2 3]

Insert to 2D array at row 2
 [[0 1]
 [2 3]
 [4 5]]

Insert to 2D array at column 2
[[0 1 4]
 [2 3 5]]

Note: If the axis is not provided, the array is flattened.


Example 3: Insert into a 2-D array Before Given Indices

import numpy as np

array1 = np.array([[0, 1], [2, 3]])
array2 = np.array([4, 5])

# insert elements from array2 before indices 0 and 1 of array1 array3 = np.insert(array1, [0, 1], array2)
print('\nInsert to 2D array before indices 0 and 1\n', array3)
# insert elements from array2 before row 0 and row 1 of array1 along axis 0 array4 = np.insert(array1, [0, 1], array2, axis=0)
print('\nInsert to 2D array before row 0 and row 1 along axis 0\n', array4)
# insert elements from array2 before column 0 and column 1 of array1 along axis 1 array5 = np.insert(array1, [0, 1], array2, axis=1)
print('\nInsert to 2D array before column 0 and column 1 along axis 1\n', array5)

Output

Insert to 2D array before indices 0 and 1
[4 0 5 1 2 3]
Insert to 2D array before row 0 and row 1 along axis 0
 [[4 5]
 [0 1]
 [4 5]
 [2 3]]

Insert to 2D array before column 0 and column 1 along axis 1
[[4 0 5 1]
 [4 2 5 3]]