Finding the average of a list

PythonListLambdaAverageReduce

Python Problem Overview


I have to find the average of a list in Python. This is my code so far

from functools import reduce

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print(reduce(lambda x, y: x + y, l))

I've got it so it adds together the values in the list, but I don't know how to make it divide into them?

Python Solutions


Solution 1 - Python

On Python 3.8+, with floats, you can use statistics.fmean as it's faster with floats.

On Python 3.4+, you can use statistics.mean:

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

import statistics
statistics.mean(l)  # = 20.11111111111111

On older versions of Python you can:

sum(l) / len(l)

On Python 2, you need to convert len to a float to get float division

sum(l) / float(len(l))

There is no need to use functools.reduce as it is much slower.

Solution 2 - Python

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
sum(l) / len(l)

Solution 3 - Python

You can use numpy.mean:

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

import numpy as np
print(np.mean(l))

Solution 4 - Python

A statistics module has been added to python 3.4. It has a function to calculate the average called mean. An example with the list you provided would be:

from statistics import mean
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
mean(l)

Solution 5 - Python

Why would you use reduce() for this when Python has a perfectly cromulent sum() function?

print sum(l) / float(len(l))

(The float() is necessary in Python 2 to force Python to do a floating-point division.)

Solution 6 - Python

There is a statistics library if you are using python >= 3.4

https://docs.python.org/3/library/statistics.html

You may use it's mean method like this. Let's say you have a list of numbers of which you want to find mean:-

list = [11, 13, 12, 15, 17]
import statistics as s
s.mean(list)

It has other methods too like stdev, variance, mode, harmonic mean, median etc which are too useful.

Solution 7 - Python

Instead of casting to float, you can add 0.0 to the sum:

def avg(l):
    return sum(l, 0.0) / len(l)

Solution 8 - Python

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:

# test mean caculation

import timeit
import statistics
import numpy as np
from functools import reduce
import pandas as pd
import math

LIST_RANGE = 10000000000
NUMBERS_OF_TIMES_TO_TEST = 10000

l = list(range(10))

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.09751558300000002
mean2 took:  0.005496791999999973
mean3 took:  0.07754683299999998
mean4 took:  0.055743208000000044
mean5 took:  0.018134082999999968
mean6 took:  0.6663848750000001
mean7 took:  0.004305374999999945
mean8 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 caculation

import timeit
import statistics
import numpy as np
from functools import reduce
import pandas as pd

LIST_RANGE = 10000000000
NUMBERS_OF_TIMES_TO_TEST = 10000

l = list(range(10))

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.17030245899968577
mean2 took:  0.002183011999932205
mean3 took:  0.09744236000005913
mean4 took:  0.07070840100004716
mean5 took:  0.022754742999950395
mean6 took:  1.6689282460001778

so clearly the winner is: sum(l) / len(l)

Solution 9 - Python

sum(l) / float(len(l)) is the right answer, but just for completeness you can compute an average with a single reduce:

>>> reduce(lambda x, y: x + y / float(len(l)), l, 0)
20.111111111111114

Note that this can result in a slight rounding error:

>>> sum(l) / float(len(l))
20.111111111111111

Solution 10 - Python

I tried using the options above but didn't work. Try this:

from statistics import mean

n = [11, 13, 15, 17, 19]

print(n)
print(mean(n))

worked on python 3.5

Solution 11 - Python

Or use pandas's Series.mean method:

pd.Series(sequence).mean()

Demo:

>>> import pandas as pd
>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> pd.Series(l).mean()
20.11111111111111
>>> 

From the docs:

> Series.mean(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)

And here is the docs for this:

> https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.mean.html

And the whole documentation:

> https://pandas.pydata.org/pandas-docs/stable/10min.html

Solution 12 - Python

I had a similar question to solve in a Udacity´s problems. Instead of a built-in function i coded:

def list_mean(n):

    summing = float(sum(n))
    count = float(len(n))
    if n == []:
        return False
    return float(summing/count)

Much more longer than usual but for a beginner its quite challenging.

Solution 13 - Python

as a beginner, I just coded this:

L = [15, 18, 2, 36, 12, 78, 5, 6, 9]

total = 0

def average(numbers):
    total = sum(numbers)
    total = float(total)
    return total / len(numbers)

print average(L)

Solution 14 - Python

If you wanted to get more than just the mean (aka average) you might check out scipy stats:

from scipy import stats
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print(stats.describe(l))

# DescribeResult(nobs=9, minmax=(2, 78), mean=20.11111111111111, 
# variance=572.3611111111111, skewness=1.7791785448425341, 
# kurtosis=1.9422716419666397)

Solution 15 - Python

In order to use reduce for taking a running average, you'll need to track the total but also the total number of elements seen so far. since that's not a trivial element in the list, you'll also have to pass reduce an extra argument to fold into.

>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> running_average = reduce(lambda aggr, elem: (aggr[0] + elem, aggr[1]+1), l, (0.0,0))
>>> running_average[0]
(181.0, 9)
>>> running_average[0]/running_average[1]
20.111111111111111

Solution 16 - Python

Both can give you close to similar values on an integer or at least 10 decimal values. But if you are really considering long floating values both can be different. Approach can vary on what you want to achieve.

>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> print reduce(lambda x, y: x + y, l) / len(l)
20
>>> sum(l)/len(l)
20

Floating values

>>> print reduce(lambda x, y: x + y, l) / float(len(l))
20.1111111111
>>> print sum(l)/float(len(l))
20.1111111111

@Andrew Clark was correct on his statement.

Solution 17 - Python

suppose that

x = [    [-5.01,-5.43,1.08,0.86,-2.67,4.94,-2.51,-2.25,5.56,1.03],
    [-8.12,-3.48,-5.52,-3.78,0.63,3.29,2.09,-2.13,2.86,-3.33],
    [-3.68,-3.54,1.66,-4.11,7.39,2.08,-2.59,-6.94,-2.26,4.33]
]

you can notice that x has dimension 3*10 if you need to get the mean to each row you can type this

theMean = np.mean(x1,axis=1)

don't forget to import numpy as np

Solution 18 - Python

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

l = map(float,l)
print '%.2f' %(sum(l)/len(l))

Solution 19 - Python

Find the average in list By using the following PYTHON code:

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print(sum(l)//len(l))

try this it easy.

Solution 20 - Python

print reduce(lambda x, y: x + y, l)/(len(l)*1.0)

or like posted previously

sum(l)/(len(l)*1.0)

The 1.0 is to make sure you get a floating point division

Solution 21 - Python

Combining a couple of the above answers, I've come up with the following which works with reduce and doesn't assume you have L available inside the reducing function:

from operator import truediv

L = [15, 18, 2, 36, 12, 78, 5, 6, 9]

def sum_and_count(x, y):
    try:
        return (x[0] + y, x[1] + 1)
    except TypeError:
        return (x + y, 2)

truediv(*reduce(sum_and_count, L))

# prints 
20.11111111111111

Solution 22 - Python

I want to add just another approach

import itertools,operator
list(itertools.accumulate(l,operator.add)).pop(-1) / len(l)

Solution 23 - Python

You can make a function for averages, usage:

average(21,343,2983) # You can pass as many arguments as you want.

Here is the code:

def average(*args):
    total = 0
    for num in args:
        total+=num
    return total/len(args)

*args allows for any number of answers.

Solution 24 - Python

numbers = [0,1,2,3]

numbers[0] = input("Please enter a number")

numbers[1] = input("Please enter a second number")

numbers[2] = input("Please enter a third number")

numbers[3] = input("Please enter a fourth number")

print (numbers)

print ("Finding the Avarage")

avarage = int(numbers[0]) + int(numbers[1]) + int(numbers[2]) + int(numbers [3]) / 4

print (avarage)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCarla DessiView Question on Stackoverflow
Solution 1 - PythonHermsView Answer on Stackoverflow
Solution 2 - PythonyprezView Answer on Stackoverflow
Solution 3 - PythonAkavallView Answer on Stackoverflow
Solution 4 - PythonMarwan AlsabbaghView Answer on Stackoverflow
Solution 5 - PythonkindallView Answer on Stackoverflow
Solution 6 - PythonChetan SharmaView Answer on Stackoverflow
Solution 7 - PythonMaxime ChéramyView Answer on Stackoverflow
Solution 8 - PythonAlon GouldmanView Answer on Stackoverflow
Solution 9 - PythonAndrew ClarkView Answer on Stackoverflow
Solution 10 - PythonNgury MangueiraView Answer on Stackoverflow
Solution 11 - PythonU12-ForwardView Answer on Stackoverflow
Solution 12 - PythonPaulo YCView Answer on Stackoverflow
Solution 13 - PythonAlmoDevView Answer on Stackoverflow
Solution 14 - PythonjasonleonhardView Answer on Stackoverflow
Solution 15 - PythonSingleNegationEliminationView Answer on Stackoverflow
Solution 16 - PythonSuperpaulView Answer on Stackoverflow
Solution 17 - PythonMohamed A M-HassanView Answer on Stackoverflow
Solution 18 - Pythonuser1871712View Answer on Stackoverflow
Solution 19 - PythonIntegraty_beastView Answer on Stackoverflow
Solution 20 - PythonRussSView Answer on Stackoverflow
Solution 21 - PythonreubanoView Answer on Stackoverflow
Solution 22 - PythonTaylanView Answer on Stackoverflow
Solution 23 - PythonPythonView Answer on Stackoverflow
Solution 24 - PythonOscar WadeView Answer on Stackoverflow