Is there a built-in or standard library method in Python to calculate the arithmetic mean (one type of average) of a list of numbers?
Advertisement
Answer
I am not aware of anything in the standard library. However, you could use something like:
JavaScript
x
8
1
def mean(numbers):
2
return float(sum(numbers)) / max(len(numbers), 1)
3
4
>>> mean([1,2,3,4])
5
2.5
6
>>> mean([])
7
0.0
8
In numpy, there’s numpy.mean()
.