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.
Delete specific 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:
📚 Discover Online Data Science Courses & Programs (Enroll for Free)
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
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 –
Remove element from array on index
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)
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.
# show memory location of arr print("Original array:", id(arr)) # show memory location of arr_new print("Returned array:", id(arr_new))
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.
Remove multiple elements based on index
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
# 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)
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.
Remove elements based on condition
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
# 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)
Original array: [1 3 4 2 5] After deletion: [1 3 5]
Here we removed all the even elements from the original array using
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.
Tutorials on numpy arrays –
- How to sort a Numpy Array?
- Create Pandas DataFrame from a Numpy Array
- Different ways to Create NumPy Arrays
- Convert Numpy array to a List – With Examples
- Append Values to a Numpy Array
- Find Index of Element in Numpy Array
- Read CSV file as NumPy Array
- Filter a Numpy Array – With Examples
- Python – Randomly select value from a list
- Numpy – Sum of Values in Array
- Numpy – Elementwise sum of two arrays
- Numpy – Elementwise multiplication of two arrays
- Using the numpy linspace() method
- Using numpy vstack() to vertically stack arrays
- Numpy logspace() – Usage and Examples
- Using the numpy arange() method
- Using numpy hstack() to horizontally stack arrays
- Trim zeros from a numpy array in Python
- Get unique values and counts in a numpy array
- Horizontally split numpy array with hsplit()