Trim zeros from a numpy array in Python

In this tutorial, we will look at how to trim zeros in python from a numpy array with the help of some examples.

Trim zeros from a numpy array

The trim_zeros() function in numpy is used to trim leading and/or trailing zeros from a one-dimensional numpy array or sequence. The following is the syntax.

import numpy as np
arr = np.trim_zeros(flit, trim='fb')

Here, “flit” is the numpy 1-d array or sequence from which you want to trim zeros and “trim” determines which zeros to trim. It is “fb” by default to remove zeros from the front and the back.

It returns the 1-d array or sequence after trimming the zeros. The input data type is preserved. For example, if you pass a list instead of a numpy array, it will return the trimmed sequence as a list.

Let’s look at some examples of the usage of the numpy trim_zeros() function.

Pass the array as an argument to the trim_zeros() function.

import numpy as np

# 1d array with leading and trailing zeros
a = np.array([0, 0, 0, 2, 4, 5, 0, 7, 3, 1, 0, 0])
# remove leading and trailing zeros
b = np.trim_zeros(a)
# display the trimmed array
print(b)

Output:

[2 4 5 0 7 3 1]

Here, we created a numpy array with zeros at the front and the end and then passed this array to the numpy trim_zeros() function. You can see that the leading and trailing zeros have been removed in the returned array.

Let’s confirm the type of b.

type(b)

Output:

numpy.ndarray

It’s a numpy array, which is expected since we passed a numpy array.

To only remove the leading zeros from the array, pass 'f' to the trim parameter. It indicates that you want to trim zeros from the front. For example, in the above array, let’s just remove only the leading zeros.

# 1d array with leading and trailing zeros
a = np.array([0, 0, 0, 2, 4, 5, 0, 7, 3, 1, 0, 0])
# remove only the leading zeros
b = np.trim_zeros(a, trim='f')
# display the trimmed array
print(b)

Output:

[2 4 5 0 7 3 1 0 0]

You can see that the returned array has all the leading zeros removed.

Similarly, you can remove just the trailing zeros from an array by passing 'b' to the trim parameter. It indicates that you want to trim from the back. Let’s see an example.

# 1d array with leading and trailing zeros
a = np.array([0, 0, 0, 2, 4, 5, 0, 7, 3, 1, 0, 0])
# remove only the trailing zeros
b = np.trim_zeros(a, trim='b')
# display the trimmed array
print(b)

Output:

[0 0 0 2 4 5 0 7 3 1]

The returned array has all the trailing zeros removed.

The numpy trim_zeros() function also allows you to trim zeros from other sequence types such as lists, tuples, etc. Also, it preserves the data type of the sequence. That is, for a list, it will return a list with the zeros trimmed.

# list with leading and trailing zeros
a = [0, 0, 0, 2, 4, 5, 0, 7, 3, 1, 0, 0]
# remove leading and trailing zeros
b = np.trim_zeros(a)
# display the trimmed list
print(b)
# diplay the type of b
print(type(b))

Output:

[2, 4, 5, 0, 7, 3, 1]
<class 'list'>

We get a list with all the leading and trailing zeros removed.

For more on the numpy trim_zeros() function, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.