×

numpy.trim_zeros() in Python

numpy.trim_zeros() in Python

The numpy.trim_zeros() trims the leading and/or trailing zeros from a 1-D array or sequence.

Syntax

numpy.trim_zeros(filt, trim='fb')

Parameter

The numpy.trim_zeros() function consists of two parameters, which are as follows:

filt : This parameter represents the 1-D array Input array.

trim : It is an optional parameter which takes a string with ‘f’ representing trim from front and ‘b’ to trim from the back. Default is ‘fb’, trim zeros from both front and back of the array.

Return

This function returns the result of trimming the input. The input data type is preserved.

Example 1

# Python program explaining
# numpy.trim_zeros() function
import numpy as np
# input array
inp_arr = np.array((0, 0, 0, 1, 2, 3, 0, 2, 1, 0))
print("Input Array: ")
print(inp_arr)
#triming the zeros in the input array
out_arr= np.trim_zeros(inp_arr)
print("After trimming the zeros: ")
print(out_arr)

Output

Input Array:
[0 0 0 1 2 3 0 2 1 0]
After trimming the zeros:
[1 2 3 0 2 1]

Related Topics

numpy.ndarray.flat() in Python

numpy.ndarray.flat() in Python The numpy.ndarray.flat() returns a 1-D iterator over the array. This function is not a subclass of, Python’s built-in iterator object, otherwise it will act the same as a...

1 minute read.

numpy.frombuffer() in Python

numpy.frombuffer() in Python The frombuffer()  function of Python numpy class creates an array by using the given buffer. Syntax numpy.frombuffer(buffer, dtype=float, count=-1, offset=0) Parameter The numpy.frombuffer() method consists of four parameters, which are as follows: buffer: This parameter...

1 minute read.

numpy.trim_zeros() in Python

numpy.trim_zeros() in Python The numpy.trim_zeros() trims the leading and/or trailing zeros from a 1-D array or sequence. Syntax numpy.trim_zeros(filt, trim='fb') Parameter The numpy.trim_zeros() function consists of two parameters, which are as follows: filt : This parameter represents the 1-D...

1 minute read.

numpy.full() in Python

numpy.full() in Python The full() method of Python numpy class returns a new array of specified shape and type, filled with fill_value. Syntax numpy.full(shape, fill_value, dtype=None, order='C') Parameter shape:This parameter represents the number of rows order :The order parameter can...

1 minute read.

numpy.arrange() in Python

Python numpy.arrange() The arrange() function of Python numpy class returns an array with equally spaced elements as per the interval where the interval mentioned is half opened, i.e. [Start, Stop). Syntax numpy.arange([start, ]stop, [step, ]dtype=None) Parameter start :It...

1 minute read.

numpy.tri() in Python

numpy.tri() in Python The tri() function of Python numpy class returns an array with ones at and below the given diagonal(k value) and zeros elsewhere. Syntax numpy.tri(N, M=None, k=0, dtype=<class 'float'>) Parameter R : It represents the number...

1 minute read.

numpy.resize() in Python

numpy.resize() in Python The numpy.resize() returns a new array with the specified shape. If the new array is larger than the original array, then the new array is filled with repeated...

1 minute read.

numpy.ascontiguousarray() in Python with Example

numpy.ascontiguousarray() in Python The ascontiguousarray() returns a contiguous array (ndim>= 1) in memory (C order). Syntax numpy.ascontiguousarray(a, dtype=None) Parameter arr : This parameter includes the input data, in any form that can be converted to an array....

1 minute read.

numpy.ravel() in Python

numpy.ravel() in Python The numpy.ravel() returns a contiguous flattened array(1-D array), containing the elements of the input. Syntax numpy.ravel(a, order='C') Parameter The numpy.ravel() method consists of two parameters, which are as follows: a: This parameter represents an Input...

2 minutes read.

Numpy.copy() in Python with Example

Numpy.copy() in Python The copy() function of Python numpy class returns an array copy for the given object. Syntax numpy.copy(a, order='K') Parameter a: It represent the array_like input data. order :  This parameter controls the memory layout of...

1 minute read.

numpy.delete() in Python

numpy.delete() in Python The numpy.delete() function returns a new array with sub-arrays along an axis deleted. For 1-D array, this function returns those entries which are not returned by arr[obj]. Syntax numpy.delete(arr, obj, axis=None) Parameter arr: This parameter returns...

2 minutes read.

numpy.asarray_chkfinite() in Python

numpy.asarray_chkfinite() in Python The numpy.asarray_chkfinite() function converts the input to an array, checking for NaNs or Infs. Syntax numpy.asarray_chkfinite(a, dtype=None, order=None) Parameter a : This parameter represents an input data, which can be in the form of lists, tuples,...

2 minutes read.

NumPy vs SciPy

NumPy- NumPy is the most important Python package for scientific computing. It's a Python library that includes a multidimensional array object, derived objects (like masked arrays and matrices), and a...

3 minutes read.

numpy.asarray() in Python

numpy.asarray() in Python The numpy.asarray()function is used to convert the input to an array. Input includes lists, lists of tuples, tuples, tuples of tuples,etc,. Syntax numpy.asarray(a, dtype=None, order=None) Parameter arr : This parameter includes theinput data, in any...

1 minute read.

numpy.eye() in Python

numpy.eye() in Python: The eye() method of Python numpy class returns a 2-D array with ones on the diagonal and zeros elsewhere. Syntax numpy.eye(N, M=None, k=0, dtype=<class 'float'>, order='C') Parameters The numpy.eye()  method consists of five parameters, which...

1 minute read.

numpy.atleast_1d() in Python

numpy.atleast_1d() in Python The numpy.atleast_1d() function converts the inputs to arrays with at least one dimension. Syntax numpy.atleast_1d(*arys) Parameter arys1, arys2, … : This parameter represents one or more input arrays. Return This function returns an array, or list...

1 minute read.

numpy.empty_like() in Python

numpy.empty_like() in Python The empty_like() method of Python numpy class returns a new array with the same shape and type as the specified array. Syntax numpy.empty_like(prototype, dtype=None, order='K', subok=True) Parameters The numpy. empty_like() method consists of four parameters,...

1 minute read.

numpy.zeros_like() in Python

Numpy.zeros_like() in Python The zeros_like() method of Python numpy class returna an array of zeros with the same shape and type as that of specified array. Syntax numpy.zeros_like(a, dtype=None, order='K', subok=True) Parameter The numpy.zeros_like() method consists of four...

1 minute read.

numpy.reshape() in Python

numpy.reshape() in Python The numpy.reshape() function gives a new shape to an array without changing its data. Syntax numpy.reshape(a, newshape, order='C') Parameter The numpy.reshape() method consists of three parameters, which are as follows: array : It represents our...

1 minute read.

Numpy.asmatrix() in Python with Example

Numpy.asmatrix() in Python The asmatrix() function returns the specified input as a matrix. It does not make a copy if the input is already a matrix or an ndarray. Syntax numpy.asmatrix(data, dtype=None) Parameter data  :This parameter...

1 minute read.