I have a NumPy array, and I want to retrieve all the elements except a certain index. For example, consider the following array
a = [0,1,2,3,4,5,5,6,7,8,9]
If I specify index 3, then the resultant should be
a = [0,1,2,4,5,5,6,7,8,9]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Like resizing, removing elements from an NumPy array is a slow operation (especially for large arrays since it requires allocating space and copying all the data from the original array to the new array).
It should be avoided if possible.
Often you can avoid it by working with a masked array instead. For example, consider the array
a:We can mask its value at index 3 and can perform a summation which ignores masked elements:
Masked arrays also support many operations besides
sum.If you really need to, it is also possible to remove masked elements using the
compressedmethod:But as mentioned above, avoid it if possible.