Find the Average of Elements in a List in Python

To find the average (mean) of elements in a list in Python, sum up all the elements and divide the total by the number of elements in the list. This can be achieved using the sum() and len() functions, or by using built-in libraries like statistics.mean(). Below are different ways to calculate the average in Python.


Examples

1. Using sum() and len()

The most common way to calculate the average of a list is by using sum() to get the total of all elements and len() to count the number of elements.

</>
Copy
# List of numbers
numbers = [10, 20, 30, 40, 50]

# Calculating average
average = sum(numbers) / len(numbers)

# Printing the average
print("Average:", average)

Explanation:

Here, we use:

  • sum(numbers) to calculate the total sum of all elements in the list.
  • len(numbers) to get the number of elements in the list.
  • Dividing the sum by the length gives the average value.

Output:

Average: 30.0

2. Using the statistics.mean() Method

The statistics module provides a built-in function mean() to calculate the average.

</>
Copy
import statistics

# List of numbers
numbers = [5, 15, 25, 35, 45]

# Calculating average using statistics.mean()
average = statistics.mean(numbers)

# Printing the average
print("Average:", average)

Explanation:

Here, we:

  • Import the statistics module.
  • Use statistics.mean(numbers) to directly compute the mean of the list.
  • This method is concise and eliminates the need for manual calculations.

Output:

Average: 25.0

3. Using a Loop to Calculate the Average

Instead of using built-in functions, we can manually calculate the average by iterating over the list.

</>
Copy
# List of numbers
numbers = [8, 12, 16, 20, 24]

# Initializing sum and count
total = 0
count = 0

# Loop to calculate sum and count elements
for num in numbers:
    total += num
    count += 1

# Calculating average
average = total / count

# Printing the average
print("Average:", average)

Explanation:

Here, we:

  • Initialize total to store the sum and count to store the number of elements.
  • Loop through the list, adding each element to total and incrementing count.
  • Calculate the average by dividing total by count using Division Operator.

Output:

Average: 16.0

4. Using reduce() from functools

The reduce() function from the functools module can be used to sum up all elements.

</>
Copy
from functools import reduce

# List of numbers
numbers = [7, 14, 21, 28, 35]

# Using reduce to calculate sum
total = reduce(lambda x, y: x + y, numbers)

# Calculating average
average = total / len(numbers)

# Printing the average
print("Average:", average)

Explanation:

Here, we:

  • Import reduce from the functools module.
  • Use reduce(lambda x, y: x + y, numbers) to sum up all elements in the list.
  • Divide the total sum by the length of the list to compute the average.

Output:

Average: 21.0

Conclusion

There are multiple ways to find the average of a list in Python:

  1. sum() / len(): The simplest and most commonly used method.
  2. statistics.mean(): A built-in method that makes calculations easy.
  3. Using a loop: Manually computes the sum and count.
  4. reduce(): A functional programming approach.