I have a list such as:
my_list = [[1,2,2,1], [0,0,1,2], [1,2,0,0], [1,0,0,1]]
I need to remove only the leading and trailing zeros from the inner lists, so that I end up with:
new_list = [[1,2,2,1], [1,2], [1,2], [1,0,0,1]]
Any help much appreciated.
for sub_list in my_list:
for dx in (0, -1):
while sub_list and sub_list[dx] == 0:
sub_list.pop(dx)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With