Cut zeros from the end of the list
list, python, python-3.x
Solution
list1 = [48, 39, 23, 15, 11, 12, 5, 9, 7, 3, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0]
list2 = [137, 30, 12, 3, 1, 0, 0, 0]
def pop_zeros(items):
while items[-1] == 0:
items.pop()
pop_zeros(list1)
pop_zeros(list2)
print(list1)
print(list2)
Output
[48, 39, 23, 15, 11, 12, 5, 9, 7, 3, 0, 0, 1, 0, 1]
[137, 30, 12, 3, 1]
Problem
function receives in argument list that have a lot of `0` at the end like: ``` [48, 39, 23, 15, 11, 12, 5, 9, 7, 3, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0] [137, 30, 12, 3, 1, 0, 0, 0] ``` If length of the list and the number of zeros at the end are always different, how can I trim it from zeroes to get ``` [48, 39, 23, 15, 11, 12, 5, 9, 7, 3, 4, 2, 0, 0, 1, 0 , 1] [137, 30, 12, 3, 1] ```