🚀 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.delete()

AI & DATA SCIENCE // np-delete

np.delete() returns a new array with sub-arrays or elements removed at specified index positions along a given axis, leaving the original array unmodified.

Syntax

np.delete(arr, obj, axis=None)

Deep Dive Course

delete() constructs a brand-new array containing everything except the specified indices, a single index, a list of indices, or a slice, along the given axis, since NumPy arrays can't shrink in place any more than they can grow in place. Without an axis argument, the array is flattened first and elements are removed from that flattened sequence, which is rarely what's wanted for multi-dimensional data, so axis should almost always be specified explicitly when working with anything beyond a 1D array.

1Understanding np.delete()

delete() constructs a brand-new array containing everything except the specified indices, a single index, a list of indices, or a slice, along the given axis, since NumPy arrays can't shrink in place any more than they can grow in place. Without an axis argument, the array is flattened first and elements are removed from that flattened sequence, which is rarely what's wanted for multi-dimensional data, so axis should almost always be specified explicitly when working with anything beyond a 1D array.

💡

Always specify the axis argument explicitly when calling np.delete() on a multi-dimensional array — omitting it flattens the array first, which almost never produces the result you actually want for 2D+ data.

editor.html
import numpy as np

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

2Practical Example

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

editor.html
import numpy as np

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

3Best Practices

Follow these guidelines when working with np.delete():

1. Always pass axis explicitly for 2D+ arrays, since the default flattens the array first, which is rarely the intended behavior

2. Use boolean indexing or a boolean mask instead of np.delete() when the condition for removal is computed dynamically, rather than a known fixed set of indices

3. Remember np.delete() always returns a new array, leaving the original untouched, unlike Python's list del statement which modifies in place

⚠️

Tip: Always specify the axis argument explicitly when calling np.delete() on a multi-dimensional array — omitting it flattens the array first, which almost never produces the result you actually want for 2D+ data.

editor.html
import numpy as np

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

Examples

Example 01Basic Usage
import numpy as np

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

matrix = np.array([[1, 2], [3, 4], [5, 6]])
result = np.delete(matrix, 1, axis=0)
print(result)

Best Practices

  • Always pass axis explicitly for 2D+ arrays, since the default flattens the array first, which is rarely the intended behavior
  • Use boolean indexing or a boolean mask instead of np.delete() when the condition for removal is computed dynamically, rather than a known fixed set of indices
  • Remember np.delete() always returns a new array, leaving the original untouched, unlike Python's list del statement which modifies in place

Interview Question

Why does forgetting the axis argument in np.delete() on a 2D array often produce a confusingly different result than intended?

Hint: Think about what happens to the array's shape when axis is omitted.

Without an explicit axis, np.delete() first flattens the array into a 1D sequence, then removes elements from that flattened version by their position in the flattened order, and returns a flat 1D result, completely losing the original 2D structure, rather than removing an entire row or column and keeping the rest of the matrix shape intact. This is almost never what someone intends when calling np.delete() on a matrix, which is why axis should be specified explicitly for anything beyond a plain 1D array.

Exercises

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

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

Frequently Asked Questions

Why does forgetting the axis argument in np.delete() on a 2D array often produce a confusingly different result than intended?

Without an explicit axis, np.delete() first flattens the array into a 1D sequence, then removes elements from that flattened version by their position in the flattened order, and returns a flat 1D result, completely losing the original 2D structure, rather than removing an entire row or column and keeping the rest of the matrix shape intact. This is almost never what someone intends when calling np.delete() on a matrix, which is why axis should be specified explicitly for anything beyond a plain 1D array.

Related Functions

np-insertnp-appendboolean-indexing