Lists are a very versatile data structure in Python. When working with a list of numbers, it can be helpful to know how to calculate the mean of list values quickly. For example, you have a list of test scores and want to know what the average score was for the test. In this tutorial, we will look at how to get the average of a list in Python with the help of some examples.
Get average of list values in Python
You can use a combination of Python
len() functions to compute the mean of a list. Alternatively, you can also use methods defined in libraries such as
numpy, etc. to get the average of a list of values.
Let’s look at the above-mentioned methods with the help of examples.
1. Mean of a list using
To compute the mean of a list, you can use the
sum() function to get the sum of the values in the list and divide that with the length of the list returned from the
# create a list ls = [1,2,3,4] # average of list sum(ls) / len(ls)
We get 2.5 as the average for the list above list of values.
You can also use the
statistics standard library in Python to get the mean of a list. Pass the list as an argument to the
import statistics # create a list ls = [1,2,3,4] # average of list statistics.mean(ls)
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
We get the same result as above.
For more on the
statistics library, refer to its documentation.
You can also use the
numpy library to get the list average. Numpy has a number of useful functions for working with arrays in Python.
import numpy as np # create a list ls = [1,2,3,4] # average of list np.mean(ls)
We get 2.5 as the average.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.