EDIT:
I added two other ways to get the average of a list (which are relevant only for Python 3.8+). Here is the comparison that I made:
import timeitimport statisticsimport numpy as npfrom functools import reduceimport pandas as pdimport mathLIST_RANGE = 10NUMBERS_OF_TIMES_TO_TEST = 10000l = list(range(LIST_RANGE))def mean1(): return statistics.mean(l)def mean2(): return sum(l) / len(l)def mean3(): return np.mean(l)def mean4(): return np.array(l).mean()def mean5(): return reduce(lambda x, y: x + y / float(len(l)), l, 0)def mean6(): return pd.Series(l).mean()def mean7(): return statistics.fmean(l)def mean8(): return math.fsum(l) / len(l)for func in [mean1, mean2, mean3, mean4, mean5, mean6, mean7, mean8 ]: print(f"{func.__name__} took: ", timeit.timeit(stmt=func, number=NUMBERS_OF_TIMES_TO_TEST))
These are the results I got:
mean1 took: 0.09751558300000002mean2 took: 0.005496791999999973mean3 took: 0.07754683299999998mean4 took: 0.055743208000000044mean5 took: 0.018134082999999968mean6 took: 0.6663848750000001mean7 took: 0.004305374999999945mean8 took: 0.003203333000000086
Interesting! looks like math.fsum(l) / len(l)
is the fastest way, then statistics.fmean(l)
, and only then sum(l) / len(l)
. Nice!
Thank you @Asclepius for showing me these two other ways!
OLD ANSWER:
In terms of efficiency and speed, these are the results that I got testing the other answers:
# test mean caculationimport timeitimport statisticsimport numpy as npfrom functools import reduceimport pandas as pdLIST_RANGE = 10NUMBERS_OF_TIMES_TO_TEST = 10000l = list(range(LIST_RANGE))def mean1(): return statistics.mean(l)def mean2(): return sum(l) / len(l)def mean3(): return np.mean(l)def mean4(): return np.array(l).mean()def mean5(): return reduce(lambda x, y: x + y / float(len(l)), l, 0)def mean6(): return pd.Series(l).mean()for func in [mean1, mean2, mean3, mean4, mean5, mean6]: print(f"{func.__name__} took: ", timeit.timeit(stmt=func, number=NUMBERS_OF_TIMES_TO_TEST))
and the results:
mean1 took: 0.17030245899968577mean2 took: 0.002183011999932205mean3 took: 0.09744236000005913mean4 took: 0.07070840100004716mean5 took: 0.022754742999950395mean6 took: 1.6689282460001778
so clearly the winner is:sum(l) / len(l)