Here's a hacky solution with iter
and zip
:
i = [1, 2, 3, 4, 5, 6]
d = iter(i)
for a, b, c in zip(*[d]*3):
print(a, b, c)
Output:
1 2 3
4 5 6
Additionally if you want it to iterate over everything when your original list isn't divisible by three you can use zip_longest
from itertools
:
from itertools import zip_longest
i = [1, 2, 3, 4, 5, 6, 7]
d = iter(i)
for a, b, c in zip_longest(*[d]*3):
print(a, b, c)
Output:
1 2 3
4 5 6
7 None None