0

I have a list of 248 values that looks like this: lst = [10.0, 7.4, 8.2, 8.8, 7.4,.... ] I want to get the average of every 8 value. Such as it will be divided to 31 sub lists and will have the average of every list.

This is what I have for now but I have to write this 31 times if I use this method. Can someone point me to an easier and more efficient way?

num1 = 0
for i in range (8):
    num += float(lst[i])
print(num1/8)


num2 = 0

for i in range (8, 16):
    num += float(lst[i])
print(num2/8)
S. Acarsoy
  • 35
  • 5

1 Answers1

1

Just use a loop:

averages = []
for start in range(0, len(lst), 8):
    num = 0
    for i in range(min(8, len(lst) - start)):
        num += lst[start + i]
    averages.append(num / 8)
Aplet123
  • 33,825
  • 1
  • 29
  • 55