I'm using Python3. I want to get 4 items from mylist
to populate list2 and print my list2 every 4 loops. And I need to print the rest even if there are just 1, 2 or 3 items at the end.
I want to do this with mod %
:
mylist = [1, 2, 3, 4, 5]
list2 = []
for count, el in enumerate(mylist):
list2.append(el)
if count % 4 == 0:
print(list2)
list2 = []
Output :
[1]
[2, 3, 4, 5]
But I need the inverse.
I tried to start at 1 enumerate(mylist, 1):
but the output is [1, 2, 3, 4]
the last is ignored.
The output that I need is :
[1, 2, 3, 4]
[5]
The length of mylist
is totally random. How can I do to get the output that I want ?