I hope this will be helpful to you. It converts a list into a list of lists
list1= [ i for i in range(0,8+1] nested =[] for i in list1: if len(nested) !=0: if i %3!=0: nested.append([i]) else: nested[len(nested)-1].append(i) else: nested.append([i]) print(nested