python trim zero off end of list 1

python trim zero off end of list

list1 = [48, 39, 23, 15, 11, 12, 5, 9, 7, 3, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0]

def pop_zeros(items):
    while items[-1] == 0:
        items.pop()

pop_zeros(list1)
pop_zeros(list2)
print(list1)

# Output:
[48, 39, 23, 15, 11, 12, 5, 9, 7, 3, 0, 0, 1, 0, 1]

Here is what the above code is Doing:
1. The pop_zeros() function takes a list as an argument.
2. The while loop checks if the last item in the list is 0.
3. If it is, the last item is removed from the list.
4. The while loop continues to run until the last item in the list is not 0.
5. The pop_zeros() function is called twice, once for each list.
6. The list is printed.

Similar Posts