bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / insert
_ArrayFunctionDispatcher
numpy:insert
source: /dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/lib/_function_base_impl.py :5379
Signature
def insert ( arr , obj , values , axis = None ) Summary
Insert values along the given axis before the given indices.
Parameters
arr: array_likeInput array.
obj: slice, int, array-like of ints or boolsObject that defines the index or indices before which
valuesis inserted.Support for multiple insertions when
objis a single scalar or a sequence with one element (similar to calling insert multiple times).values: array_likeValues to insert into
arr. If the type ofvaluesis different from that ofarr,valuesis converted to the type ofarr.valuesshould be shaped so thatarr[...,obj,...] = valuesis legal.axis: int, optionalAxis along which to insert
values. Ifaxisis None thenarris flattened first.
Returns
out: ndarrayA copy of
arrwithvaluesinserted. Note that insert does not occur in-place: a new array is returned. Ifaxisis None, out is a flattened array.
Notes
Note that for higher dimensional inserts obj=0 behaves very different from obj=[0] just like arr[:,0,:] = values is different from arr[:,[0],:] = values. This is because of the difference between basic and advanced indexing <basics.indexing>.
Examples
import numpy as np a = np.arange(6).reshape(3, 2) a np.insert(a, 1, 6) np.insert(a, 1, 6, axis=1)✓
np.insert(a, [1], [[7],[8],[9]], axis=1) np.insert(a, 1, [[7],[8],[9]], axis=1) np.array_equal(np.insert(a, 1, [7, 8, 9], axis=1), np.insert(a, [1], [[7],[8],[9]], axis=1))✓
b = a.flatten() b np.insert(b, [2, 2], [6, 7])✓
np.insert(b, slice(2, 4), [7, 8])
✓np.insert(b, [2, 2], [7.13, False]) # type casting
✓x = np.arange(8).reshape(2, 4) idx = (1, 3) np.insert(x, idx, 999, axis=1)✓
See also
- append
Append elements at the end of an array.
- concatenate
Join a sequence of arrays along an existing axis.
- delete
Delete elements from an array.
Aliases
-
numpy.insert