I have a list
data = [1, 2, 3, 0, 0, 4, 5, 0, 0, 0, 4, 6, 7, 0]
expected output
result = [[1,2,3], [4,5], [4,6,7]]
Split the list when a zero occur; add all the non-zero numbers to a new list till the next zero
This is what I have tried:
res = []
tmp = []
for i in data:
if i == 0 and len(tmp) > 0:
res.append(tmp)
tmp = []
elif i != 0:
tmp.append(i)
I am wondering is there a pythonic way of doing the same...