how can i write a loop in a simpler way?

Use sum with a generator expression:

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
my_list_average = sum(my_list) / len(my_list)

n = sum(pow(my_list_average - i, 2) for i in my_list)
print(n)

Output

60.0

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top