insert() always returns a new array, since NumPy arrays can't be resized in place — it copies elements before the insertion point, places the new values, then copies the remaining elements after them. The obj argument can be a single index or a list of indices, and multiple insertion points are all computed relative to the original array's positions, not the growing result, so inserting at the same index twice inserts two separate values right next to each other at what was originally that position.
1Understanding np.insert()
insert() always returns a new array, since NumPy arrays can't be resized in place — it copies elements before the insertion point, places the new values, then copies the remaining elements after them. The obj argument can be a single index or a list of indices, and multiple insertion points are all computed relative to the original array's positions, not the growing result, so inserting at the same index twice inserts two separate values right next to each other at what was originally that position.
Like np.append(), np.insert() allocates an entirely new array every time it's called — avoid calling it repeatedly inside a loop for the same reason repeated append() calls are inefficient.
import numpy as np
arr = np.array([1, 2, 4, 5])
result = np.insert(arr, 2, 3)
print(result)2Practical Example
Here is a real-world application of np.insert() showing how it is used in production NumPy code.
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
result = np.insert(matrix, 1, [9, 9], axis=0)
print(result)3Best Practices
Follow these guidelines when working with np.insert():
1. Avoid calling np.insert() repeatedly inside a loop, for the same reason repeated np.append() calls are inefficient — build up the data differently and construct the array once
2. Pass a list of indices to insert multiple values in a single call instead of calling insert() multiple times
3. Specify the axis parameter explicitly for multi-dimensional arrays, since omitting it flattens the array first, which is rarely what's intended for 2D+ data
Tip: Like np.append(), np.insert() allocates an entirely new array every time it's called — avoid calling it repeatedly inside a loop for the same reason repeated append() calls are inefficient.
import numpy as np
arr = np.array([1, 2, 4, 5])
result = np.insert(arr, 2, 3)
print(result)