NumPy insert(): Insert Values at Given Index Position & Axis

numpy.insert(arr, obj, values, axis=None) inserts values along the given axis before the given indices. Unlike append() which only adds elements to the end of an array, insert() allows precise placement at arbitrary index positions.

Note that insert() always returns a copy of the input array rather than modifying the original array in place. If no axis is specified, the input array is flattened prior to insertion. For related structural array modifications, review append() and reshape().

ParameterDescription
arrInput array. If axis is None, arr is flattened first
objObject specifying the index or indices before which values is inserted
valuesValues to insert into arr. Must be broadcast-compatible with the insertion shape
axisAxis along which to insert values. If None, arr is flattened

Syntax and Parameters 🔝

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

Code Block Modernization Comparison 🔝

Review how legacy NumPy insert snippets compare against the modernized, structured, and self-verifying implementation:

Aspect Legacy Implementation Modernized Implementation
Code Structure Unformatted echo statements, mixed tutorial content, no assertions. Structured HTML5 layout, Schema TechArticle markup, and responsive tables.
Error Handling Minimal explanation of shape mismatch and broadcast errors. Detailed explanation of ValueError shape rules and broadcasting requirements.
Verification Unchecked print statements with manual result inspection comments. Automated unit test assertions (assert) verifying shape and array contents.
Ecosystem Integration Isolated code blocks with sparse navigation links. Comprehensive cross-linking, quick-jump navigation, and runnable Colab notebooks.

Basic 1D Insertion 🔝

Insert a single value at a specified index position in a 1D array. Note that the original array remains unchanged as insert() returns a new copy:

import numpy as np

npr = np.array([5, 8, 3])
npr1 = np.insert(npr, 1, 10) # Insert 10 at index position 1

print("Original array:", npr)   # [5 8 3] (unchanged)
print("Inserted array:", npr1)  # [ 5 10  8  3]

assert np.array_equal(npr, np.array([5, 8, 3]))
assert np.array_equal(npr1, np.array([5, 10, 8, 3]))

Expected output

Original array: [5 8 3]
Inserted array: [ 5 10  8  3]

Inserting at the End 🔝

You can append an element at the end of the array by setting the insertion index to len(arr):

npr = np.array([5, 8, 3])
npr1 = np.insert(npr, len(npr), 10)

print("Inserted at end:", npr1) # [ 5  8  3 10]
assert npr1[-1] == 10

Expected output

Inserted at end: [ 5  8  3 10]

2D Array Insertion & Shape Rules 🔝

When an axis is provided, the inserted values must be broadcast-compatible with the dimensions of the array. Mismatched shapes will raise a ValueError:

npr = np.array([
    [0, 1, 2, 4],
    [3, 4, 5, 6],
    [6, 7, 8, 9]
])

# Insert a row along axis=0
npr1 = np.insert(npr, 1, [10, 11, 12, 13], axis=0)
print("Inserted row:
", npr1)

assert npr1.shape == (4, 4)

Expected output

Inserted row:
 [[ 0  1  2  4]
 [10 11 12 13]
 [ 3  4  5  6]
 [ 6  7  8  9]]

Axis Direction & Multiple Indices 🔝

You can insert columns along axis=1 or specify multiple insertion indices simultaneously:

npr = np.array([
    [0, 1, 2, 4],
    [3, 4, 5, 6],
    [6, 7, 8, 9]
])

# Insert column along axis=1
npr_col = np.insert(npr, 1, [10, 11, 12], axis=1)
print("Inserted column:
", npr_col)

assert npr_col.shape == (3, 5)

Expected output

Inserted column:
 [[ 0 10  1  2  4]
 [ 3 11  4  5  6]
 [ 6 12  7  8  9]]

Flattened Insertion without Axis 🔝

If axis is omitted, NumPy flattens the multi-dimensional array before performing the insertion:

npr = np.array([
    [0, 1, 2, 4],
    [3, 4, 5, 6],
    [6, 7, 8, 9]
])

npr_flat = np.insert(npr, 1, [10, 11, 12])
print("Flattened insertion result:
", npr_flat)
assert npr_flat.ndim == 1

Expected output

Flattened insertion result:
 [ 0 10 11 12  1  2  4  3  4  5  6  6  7  8  9]

Practice in Google Colab 🔝

Test array insertions, axis specifications, shape broadcasting rules, and run verification test suites in our interactive Google Colab companion notebook:

Interactive Practice: Colab & GitHub

Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:

View on GitHub Open In Colab

Continue exploring related array manipulation functions: array(), append(), array_split(), and reshape().




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer