numpy.trim_zeros function is used to trim the leading and/or trailing zeros from a 1-D array or sequence.
Syntax: numpy.trim_zeros(arr, trim)
arr : 1-D array or sequence
trim : trim is an optional parameter with default value to be ‘fb'(front and back) we can either select ‘f'(front) and ‘b’ for back.
Returns: trimmed : 1-D array or sequence (without leading and/or trailing zeros as per user’s choice)
Output :array([1, 5, 7, 0, 6, 2, 9, 0, 10])
Output :array([1, 5, 7, 0, 6, 2, 9, 0, 10, 0, 0])
Output :array([0, 0, 0, 0, 1, 5, 7, 0, 6, 2, 9, 0, 10])
- Python | Merge Python key values to list
- Python | Index of Non-Zero elements in Python list
- Reading Python File-Like Objects from C | Python
- Python | Convert list to Python array
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to Python Libraries
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Visualizing O(n) using Python
- Any & All in Python
- try and except in Python
- SQL using Python | Set 1
- try-except vs If in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.