The trim_zeros() function in Python is used to trim out the leading zeros from an input 1-D array.
The trim_zeros() function takes the syntax below:
numpy.trim_zeros(filt, trim='fb')
The trim_zeros() function takes the following parameter values:
filt: This is the input array. It is a required parameter.trim: This is a string with an 'f', which represents trim from the front, and 'b' , which represents trim from the back. This is an optional parameter.The trim_zeros() function returns an array whose leading zero entry(ies) is trimmed.
import numpy as np# creating an input arraya = np.array([0, 0, 0, 1, 2, 3, 4, 5, 0, 0, 0])print(a)# trimming the zero entries from fromtprint(np.trim_zeros(a))# trimming the zero entries from fromtprint(np.trim_zeros(a, "b"))
numpy module.a , using the array() function.a.trim_zeros() function. We print the result to the console.trim_zeros() function. We print the result to the console.