🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.insert()

AI & DATA SCIENCE // np-insert

np.insert() returns a new array with values inserted at specified index positions along a given axis, shifting later elements to make room.

Syntax

np.insert(arr, obj, values, axis=None)

Deep Dive Course

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.

editor.html
import numpy as np

arr = np.array([1, 2, 4, 5])
result = np.insert(arr, 2, 3)
print(result)
localhost:3000

2Practical Example

Here is a real-world application of np.insert() showing how it is used in production NumPy code.

editor.html
import numpy as np

matrix = np.array([[1, 2], [3, 4]])
result = np.insert(matrix, 1, [9, 9], axis=0)
print(result)
localhost:3000

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.

editor.html
import numpy as np

arr = np.array([1, 2, 4, 5])
result = np.insert(arr, 2, 3)
print(result)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([1, 2, 4, 5])
result = np.insert(arr, 2, 3)
print(result)
Example 02Advanced Example
import numpy as np

matrix = np.array([[1, 2], [3, 4]])
result = np.insert(matrix, 1, [9, 9], axis=0)
print(result)

Best Practices

  • 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
  • Pass a list of indices to insert multiple values in a single call instead of calling insert() multiple times
  • 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

Interview Question

If you insert values at index 1 twice in a row into the array [1, 2, 3], where do the two new values end up relative to each other?

Hint: Think about whether the second index is computed relative to the original array or the already-growing result.

Both index positions passed to np.insert() are interpreted relative to the original array's positions, not a progressively growing intermediate result, so inserting at index 1 twice places both new values right next to each other, immediately before the original element that was at index 1. The two insertions don't push each other further apart the way you might expect if each insertion shifted the reference point for the next one.

Exercises

MediumPractice using np.insert() in a real scenario.
View Solution
import numpy as np

arr = np.array([1, 2, 4, 5])
result = np.insert(arr, 2, 3)
print(result)

Frequently Asked Questions

If you insert values at index 1 twice in a row into the array [1, 2, 3], where do the two new values end up relative to each other?

Both index positions passed to np.insert() are interpreted relative to the original array's positions, not a progressively growing intermediate result, so inserting at index 1 twice places both new values right next to each other, immediately before the original element that was at index 1. The two insertions don't push each other further apart the way you might expect if each insertion shifted the reference point for the next one.

Related Functions

np-appendnp-deletenp-concatenate