Calculating arithmetic mean (one type of average) in Python

The Solution to Calculating arithmetic mean (one type of average) in Python is


I am not aware of anything in the standard library. However, you could use something like:

def mean(numbers):
    return float(sum(numbers)) / max(len(numbers), 1)

>>> mean([1,2,3,4])
2.5
>>> mean([])
0.0

In numpy, there's numpy.mean().

~ Answered on 2011-10-10 17:22:04


Most Viewed Questions: