How to remove specific elements from a NumPy array ?
In this article, we will discuss how to remove specific element from the NumPy Array. The delete() method will be used to do the same.
Where array_name is the name of the array to be deleted and index-value is the index of the element to be deleted.
For example, we are having an array with 5 elements,
The indexing starts from 0 to n-1. If we want to delete 2, then 2 element index is 1. So, we can specify
If we want to delete multiple elements i.e. 1,2,3,4,5 at a time, you can specify all index elements in a list.
Below are some examples where we remove specific elements in a NumPy array.
Program to create an array with 5 elements and delete 1st element.
[1 2 3 4 5] remaining elements after deleting 1st element [2 3 4 5]
Program to create an array with 5 elements and delete 1st and last element.
[1 2 3 4 5] remaining elements after deleting 1st and last element [2 3 4]
Deleting 4th element.
[ 1 2 3 4 5 6 7 8 9 10]
remaining elements after deleting 4th element [ 1 2 3 5 6 7 8 9 10]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course