Can you help me with this code?
s = [1, 1, 3, 3, 2, 2, 2, 2, 1, 1, 2, 2, 2]
def group(s):
lst = []
temp_lst = []
for i in s:
if len(temp_lst) == 0:
temp_lst.append(i)
continue
if temp_lst[0] == i:
temp_lst.append(i)
else:
lst.append(temp_lst)
del temp_lst[:]
temp_lst.append(i)
return lst
It returns:
[[2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2]]
Why?
My desired output is:
[[1, 1], [3, 3], [2, 2, 2, 2], [1, 1], [2, 2, 2]]