a = [1,2,23,4,5,6,7,40,9,10,100,12,13,14,15,700,17,18,19,20]
for i in range(0, len(a), 5):
print(sum(a[i:i+5]))
a = [1,2,23,4,5,6,7,40,9,10,100,12,13,14,15,700,17,18,19,20]
i = 5
while i < len(a):
a.insert(i, 33333335)
i += 6
print (a)
def generate(sequence, group_by=5, func=sum):
for group in zip(*[iter(sequence)]*group_by):
yield from group
yield func(group)
print(*generate(a))
a = [1, 2, 23, 4, 5, 6, 7, 40, 9, 10, 100, 12, 13, 14, 15, 700, 17, 18, 19, 20]
for index, group in reversed(list([(i, a[i:i+5]) for i in range(0, len(a), 5)])):
s = sum(group)
a.insert(index + 5, s)
print(f'{index} {group} => {s}')
# 15 [700, 17, 18, 19, 20] => 774
# 10 [100, 12, 13, 14, 15] => 154
# 5 [6, 7, 40, 9, 10] => 72
# 0 [1, 2, 23, 4, 5] => 35
# a
# [1, 2, 23, 4, 5, 35, 6, 7, 40, 9, 10, 72, 100, 12, 13, 14, 15, 154, 700, 17, 18, 19, 20, 774]
def sumelem(seq, step):
for i, elem in enumerate(seq,1):
if i%step:
yield elem
else:
yield elem
yield sum(seq[i-step:i])
print(*sumelem(a, 5))