Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (16.4k points)

Consider a list of values in it. How to find the average of a list in python?

Look at my code:

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

print reduce(lambda x, y: x + y, l)

This will add the entire number, But I don't know how to divide them. Can anyone suggest to me some ways to find the average?

1 Answer

0 votes
by (26.4k points)

From Python 3.4+ versions, you can use statistics.mean()

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

import statistics

statistics.mean(l)  # 20.11111111111111

In case of older versions, you can do 

sum(l) / len(l)

In case of python 2, you have to convert len to a float.

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

Want to learn more about Python? Come and join: python certification course

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer

Browse Categories

...