remove elements from numpy array

How to remove elements from a numpy array?

In this tutorial, we will look at how to remove elements from a numpy array based on their index with the help of simple examples.

Remove elements from numpy array

You can use the np.delete() function to remove specific elements from a numpy array based on their index. The following is the syntax:

import numpy as np
# arr is a numpy array
# remove element at a specific index
arr_new = np.delete(arr, i)
# remove multiple elements based on index
arr_new = np.delete(arr, [i,j,k])

Note that, technically, numpy arrays are immutable. That is, you cannot change them once they are created. The np.delete() function returns a copy of the original array with the specific element deleted.

Let’s look at some examples to clearly see this in action –

Pass the array and the index of the element that you want to delete.

import numpy as np

# create a numpy array
arr = np.array([1, 3, 4, 2, 5])
# remove element at index 2
arr_new = np.delete(arr, 2)
# display the arrays
print("Original array:", arr)
print("After deletion:", arr_new)

Output:

Original array: [1 3 4 2 5]
After deletion: [1 3 2 5]

Here, we created a one-dimensional numpy array and then removed the element at index 2 (that is, the third element in the array, 4). We see that the returned array does not have 4.

Let’s see if the returned array object is the same as the original array.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# show memory location of arr
print("Original array:", id(arr))
# show memory location of arr_new
print("Returned array:", id(arr_new))

Output:

Original array: 1931568517328
Returned array: 1931564130016

We can see that the original array and the returned array from the np.delete() point to different locations, that is, they are both different objects. This implies that the original array was not technically modified and rather a copy of the original array with the element deleted was returned.

You can remove multiple elements from the array based on their indexes. For this, pass the indexes of elements to be deleted as a list to the np.delete() function.

# remove element at index 2, 4
arr_new = np.delete(arr, [2, 4])
# display the arrays
print("Original array:", arr)
print("After deletion:", arr_new)

Output:

Original array: [1 3 4 2 5]
After deletion: [1 3 2]

Here, we removed elements at index 2 and 4 from the original array. See that the returned array doesn’t have elements 4 and 5 which are present at indexes 2 and 4 in the original array respectively.

Another important use case of removing elements from a numpy array is removing elements based on a condition. Use np.where() to get the indexes of elements to remove based on the required condition(s) and then pass it to the np.delete() function.

# create a numpy array
arr = np.array([1, 3, 4, 2, 5])
# remove all even elements from the array
arr_new = np.delete(arr, np.where(arr%2 == 0))
# display the arrays
print("Original array:", arr)
print("After deletion:", arr_new)

Output:

Original array: [1 3 4 2 5]
After deletion: [1 3 5]

Here we removed all the even elements from the original array using np.where() and np.delete().

For more on the np.delete() function, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top