numpy.delete() in Python (original) (raw)
Last Updated : 23 Jan, 2026
The numpy.delete() function returns a new array with the deletion of sub-arrays along with the mentioned axis.
Python `
import numpy as np
a = np.array([10, 20, 30, 40]) r = np.delete(a, 1)
print(r)
`
Here, the element at index 1 is removed from the array.
Syntax
numpy.delete(array, object, axis = None)
**Parameters:
- **array: [array_like]Input array.
- **object: [int, array of ints]Sub-array to delete
- **axis: Axis along which we want to delete sub-arrays. By default, it object is applied to flattened array
**Return: A new NumPy array with the specified elements removed.
Let's look at some of the examples:
Deletion from 1D array
Python `
import numpy as np
arr = np.arange(5) print("Array:", arr) print("Shape:", arr.shape)
Delete a single element
obj = 2 a = np.delete(arr, obj) print("\nDeleting index {}: {}".format(obj, a)) print("Shape:", a.shape)
Delete multiple elements
obj = [1, 2] b = np.delete(arr, obj) print("\nDeleting indices {}: {}".format(obj, b)) print("Shape:", b.shape)
`
Output
('Array:', array([0, 1, 2, 3, 4])) ('Shape:', (5,))
Deleting index 2: [0 1 3 4] ('Shape:', (4,))
Deleting indices [1, 2]: [0 3 4] ('Shape:', (3,))
**Explanation:
- np.arange(5) creates a 1D array with values from 0 to 4.
- np.delete(arr, 2) removes the element at index 2.
- np.delete(arr, [1, 2]) removes elements at indices 1 and 2.
- Each call returns a new array with reduced size.
Deletion from a 2D Array
Python `
import numpy as np
arr = np.arange(12).reshape(3, 4) print("Array:\n", arr) print("Shape:", arr.shape)
Delete row at index 1
a = np.delete(arr, 1, axis=0) print("\nAfter deleting row 1:\n", a) print("Shape:", a.shape)
Delete column at index 1
b = np.delete(arr, 1, axis=1) print("\nAfter deleting column 1:\n", b) print("Shape:", b.shape)
`
**Output:
Array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Shape: (3, 4)After deleting row 1:
[[ 0 1 2 3]
[ 8 9 10 11]]
Shape: (2, 4)After deleting column 1:
[[ 0 2 3]
[ 4 6 7]
[ 8 10 11]]
Shape: (3, 3)
**Explanation:
- reshape(3, 4) converts the array into 3 rows and 4 columns.
- axis=0 deletes an entire row at index 1.
- axis=1 deletes an entire column at index 1.
- The shape of the array changes based on the axis used.
Deletion Using a Boolean Mask
Python `
import numpy as np
a = np.arange(5) print("Original array:", a)
mask = np.ones(len(a), dtype=bool) mask[[0, 2]] = False
print("Mask:", mask)
res = a[mask] print("After deletion:", res)
`
Output
('Original array:', array([0, 1, 2, 3, 4])) ('Mask:', array([False, True, False, True, True])) ('After deletion:', array([1, 3, 4]))
**Explanation:
- mask is a boolean array controlling which elements to keep.
- False values indicate elements to exclude.
- Boolean indexing filters the array without using **np.delete().