words=["I","like","chocolate","I","like","strawberry"]
for i,l in enumerate(words,start=1):
print(i)
This a simple code and what I would like to have the following output (i/o the one from the presented code):
[[1,2,3],[4,5,6]]
Meaning, I want a list composed of two sublists of n=3 with the indexes that came from the enumerate.
Is this possible? I believe it might be easy, but even after searching for an answer, nothing was good enough to provide me with what I need. Thanks in advance
Taking a stab at it and hopefully the simple solution works for you. Keep 2 lists one for enumerating and one for keeping the results. Keep checking length of one list and when it reaches the threshold append to results.
words=["I","like","chocolate","I","like","strawberry"]
l=[]
result=[]
for i,w in enumerate(words,start=1):
l.append(i)
if len(l) == 3:
result.append(l)
l = []
if l:
result.append(l) # [[1, 2, 3], [4, 5, 6]]
print(result)