If you are using Python 3.4 or above, you could use the statistics
module:
from statistics import mean
average = mean(value[1] for value in array)
Or if you're using a version of Python older than 3.4:
average = sum(value[1] for value in array) / len(array)
These solutions both use a nice feature of Python called a generator expression. The loop
value[1] for value in array
creates a new sequence in a timely and memory efficient manner. See PEP 289 -- Generator Expressions.
If you're using Python 2, and you're summing integers, we will have integer division, which will truncate the result, e.g:
>>> 25 / 4
6
>>> 25 / float(4)
6.25
To ensure we don't have integer division we could set the starting value of sum
to be the float
value 0.0
. However, this also means we have to make the generator expression explicit with parentheses, otherwise it's a syntax error, and it's less pretty, as noted in the comments:
average = sum((value[1] for value in array), 0.0) / len(array)
It's probably best to use fsum
from the math
module which will return a float
:
from math import fsum
average = fsum(value[1] for value in array) / len(array)