I have to find the average of a list in Python. This is my code so far
l = [15, 18, 2, 36, 12, 78, 5, 6, 9] print reduce(lambda x, y: x + y, l)
sum(l) / float(len(l)) is the right answer, but just for completeness you can compute an average with a single reduce:
sum(l) / float(len(l))
>>> 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