w3resource logo

:

Python Exercises

Python NumPy: Remove specific elements in a numpy array

Python NumPy: Array Object Exercise-89 with Solution

Write a Python program to remove specific elements in a numpy array.

Sample Solution:-

Python Code:

import numpy as np
x = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
index = [0, 3, 4]
print("Original array:")
print(x)
print("Delete first, fourth and fifth elements:")
new_x = np.delete(x, index)
print(new_x)

Sample Output:

Original array:                                                        
[ 10  20  30  40  50  60  70  80  90 100]                              
Delete first, fourth and fifth elements:                               
[ 20  30  60  70  80  90 100]

Python Code Editor:

Improve this sample solution and post your code through Disqus