Open In App

Find Sum and Average of List in Python

Last Updated : 01 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Our goal is to find sum and average of List in Python. The simplest way to do is by using a built-in method sum() and len(). For example, list of numbers is, [10, 20, 30, 40, 50] the sum is the total of all these numbers and the average is the sum divided by the number of elements in the list.

Using sum() and len()

Python provides convenient built-in functions like sum() and len() to quickly calculate the sum and length of list respectively.

Python
a = [10, 20, 30, 40, 50]

sum1 = sum(a)
avg = s / len(a) if a else 0  

print(sum1)
print(avg)

Output
Sum of the list: 150
Average of the list: 30.0

Explanation:

  • sum(a): Calculates sum of numbers in the list ‘a‘.
  • len(a): Returns number of elements in the list ‘a‘.
  • av = sum1 / len(a): This compute the average. We also check if the list is not empty to avoid division by zero.

Using Loop

If we’d like to manually calculate the sum and average without using built-in functions then we can find this by looping through the list.

Python
a = [10, 20, 30, 40, 50]

sum1 = 0
l = 0

for val in a:
    sum1 += val
    l += 1

avg = sum1 / l if a else 0  

print(sum1)
print(avg)

Output
Sum of the list: 150
Average of the list: 30.0

Explanation: We iterate over the list ‘a’ and calculate their sum and length. And to find the average divide total calculated sum by length of list.

Related Articles:



Next Article

Similar Reads