How to remove zeros from NumPy array

You can remove zeros from the NumPy array with the following code. If you want to learn Python, I highly recommend reading This Book.

Remove zeros from NumPy array

# Import the NumPy library as np
import numpy as np

# Initialize the NumPy array
a = np.array([0, 10, 0, 20, 25, 0, 35, 0, 45, 50])

# Remove Zeros from NumPy Array
b = a[a != 0]

# Display the array after removing zeros
print(b)

Output:

[10 20 25 35 45 50]

Free Learning Resources

Leave a Comment

Your email address will not be published.