lst = ['a', 'b', 'c', 'd', 'e', 'i', 'u', 'y', 'l', 'p']
if(len(lst)%3==0):
pointer = 0
lst1 = []
for i in lst:
for j in range(pointer, pointer+3):
lst1.append(lst[j])
print(lst1)
pointer += 3
else:
counter = 3
pointer = 0
lst1 = []
for i in lst:
for j in range(pointer, pointer+3):
lst1.append(lst[j])
counter -= 1
pointer+=3
if (counter != 0):
lst1.append(lst)
pointer+=1
counter-=1
print('nothing')
The error is related to the lst1.append(lst[j]) which is out of range, but I can not find any other way to fix it. The code is supposed to take a list if it is divisible by 3, divide it into batches with 3 components, and if not, take some element from the beginning of the list.