- Average of List in Python ( 7 Examples)
- 1. Quick Examples of Average of a List
- 2. Average of List in Python using sum() & len()
- 2.1 Syntax
- 2.2 Python List Average Example
- 3. Using mean() from statistics module
- 3.1 Syntax
- 3.2 Average List Example using mean()
- 4. Get List Average using functools.reduce()
- 4.1 Syntax
- 4.2 Example
- 5. Using reduce with Lambda Expression
- 5.1 Syntax
- 5.2 Example
- 6. Using numpy Module to get List Average
- 6.1 Syntax
- 6.2 Examples
- 7. Using for loop to get List Average
- 7.1 Example
- 8. Using a while loop
- 8.1 Example
- 9. Conclusion
- You may also like reading:
- Python – Find Average of values in a List
- Get average of list values in Python
- 1. Mean of a list using sum() and len()
- 2. Using statistics library
- 3. Using numpy library
- Author
Average of List in Python ( 7 Examples)
How to find the average or mean of elements in the list in Python? A mean is defined as the mathematical average of two or more values. There are different ways to calculate the average of all the number elements in the list. for example, you can use the sum() built-in function along with len(). Besides this, there are several other ways to get the average of the list.
Below are the methods to calculate the average of elements from the list in Python.
- Method 1: Using Looping like for loop and while loop.
- Method 2: Using sum(), len() built-in functions
- Method 3: Using mean() from statistics and numpy modules
- Method 4: Using average() from numpy module
- Method 5: Using reduce() with lambda expression and operator.add() method.
1. Quick Examples of Average of a List
Following are quick examples of getting the average or mean of elements in the list.
from functools import reduce import statistics from operator import add import numpy # Consider the list of integers scores=[100,200,300,400,500,120,450] print("Actual List: ",scores) # Example 1: Using sum() and len() print("Average Score: ",sum(scores) / len(scores)) # Example 2: Using lambda expression with reduce() print("Average Score: ",reduce(lambda i, j: i + j, scores) / len(scores)) # Example 4: Using add() from operator module with reduce() print("Average Score: ",reduce(add, scores)/len(scores)) # Example 3: Using mean() from statistics module print("Average Score: ",statistics.mean(scores)) # Example 5: Using mean() from numpy module print("Average Score: ",numpy.mean(scores)) # Example 6: Using average() from numpy module print("Average Score: ",numpy.average(scores)) # Example 7: Using for loop total=0 for i in scores: total=total+i print("Average Score: ",total/len(scores)) # Example 8: Using while loop total=0 i=0 while (i < len(scores)): total=total+scores[i] i=i+1 print("Average Score: ",total/len(scores))
2. Average of List in Python using sum() & len()
Let’s get the average of the list in Python using sum() & len() functions. In general, the average is calculated by getting the sum of the elements in the list and dividing it by the count of elements. The sum() is a Python in-built function that will return the total sum of elements from the list and the len() will return the total number of elements present in the list.
If we divide the result of sum() and the result of len() function we can get the average of elements present in the list. Both functions take the list as a parameter.
2.1 Syntax
Let’s see the syntax of how to get the average of elements in a list using sum() and len().
# Syntax sum(mylist1 ) / len(mylist1 )
2.2 Python List Average Example
Consider the list that holds 7 integers and return the average of these elements using sum() and len().
# Consider the list of integers scores=[100,200,300,400,500,120,450] print("Actual List: ",scores) # Using sum() and len() print("Average Score: ",sum(scores) / len(scores))
This example yields the below output.
The sum of elements is 2070 and the total elements in the list are 7 , So the average is 2070/7 => 295.7142857142857 .
3. Using mean() from statistics module
In the above example, we have calculated the average manually by using the other methods. Let’s use the existing method to get the same result. In python, the statistics module provides a mean() function which will return the average of elements present in the list.
3.1 Syntax
Let’s see the syntax of how to return the average of elements in a list using mean() from the statistics module.
# Syntax statistics.mean(mylist1)
Here, mylist1 is the input list.
3.2 Average List Example using mean()
Consider the list that holds 3 integers and return the average of these elements. Here, The sum of elements is 600 and the total elements in the list are 3 , So the average is 600/3 => 200 .
# Import module import statistics # Consider the list of integers scores=[100,200,300] print("Actual List: ",scores) # Using mean() from statistics module print("Average Score: ",statistics.mean(scores))
This example yields the below output.
4. Get List Average using functools.reduce()
The reduce() function from functools module will reduce the iterable/sequence by applying a mathematical expression. Here, we will pass add() method from the operator module, which will return the sum of all elements in the list and divide it by the total number of elements in the list by using len() function.
4.1 Syntax
Let’s see the syntax of using reduce() & operator.add() methods.
# Syntax reduce(operator.add, mulist1)/len(mulist1)
4.2 Example
Consider the list that holds 5 integers and return the average of these elements using reduce() by passing add() method as a parameter.
# Import modules from functools import reduce from operator import add # Consider the list of integers scores=[100,200,300,678,90] print("Actual List: ",scores) # Using reduce() add() print("Average Score: ",reduce(add, scores)/len(scores)) # Output: # Actual List: [100, 200, 300, 678, 90] # Average Score: 273.6
5. Using reduce with Lambda Expression
Here, we will use lambda expression as a mathematical expression that will return the average of elements present in the python list. We will first add numbers and then divide this sum by the total number of elements present in the list.
5.1 Syntax
Let’s see the syntax of using lambda expression in reduce().
# Syntax reduce(lambda i, j: i + j, mylist1) / len(mylist1)
5.2 Example
Consider the list that holds 6 integers and return the average of these elements using reduce() by passing lambda expression as a parameter.
from functools import reduce # Consider the list of integers scores=[1,2,3,4,5,6] print("Actual List: ",scores) # Using lambda expression with reduce() print("Average Score: ",reduce(lambda i, j: i + j, scores) / len(scores)) # Output: # Actual List: [1, 2, 3, 4, 5, 6] # Average Score: 3.5
6. Using numpy Module to get List Average
The NumPy library is a popular open-source library used for scientific computing applications, and it stands for Numerical Python, which is consisting of multidimensional array objects and a collection of routines for processing those arrays.
The numpy module supports two functions that will return the average of values in the list which are mean() and average(), you can use any of these to get the average of the python list.
6.1 Syntax
Let’s see the syntax of how to return the average of elements in a list using mean() from the numpy module.
# Using mean() numpy.mean(mylist1) # Using average() numpy.average(mylist1)
6.2 Examples
Example 1: Consider the list that holds 3 integers and return the average of these elements using numpy.mean().
# Import numpy import numpy # Consider the list of integers scores=[100,200,300] print("Actual List: ",scores) # Using mean() from numpy module print("Average Score: ",numpy.mean(scores)) # Output: # Actual List: [100, 200, 300] # Average Score: 200.0
Example 2: Consider the list that holds 3 integers and return the average of these elements using numpy.average().
# Import numpy import numpy # Consider the list of integers scores=[100,200,300] print("Actual List: ",scores) # Using average() from numpy module print("Average Score: ",numpy.average(scores)) # Output: # Actual List: [100, 200, 300] # Average Score: 200.0
7. Using for loop to get List Average
Here, we will get a value from the list for each iteration done using for loop and add each element to the variable. The sum is stored in this variable. Now we will use the len() function to return the total number of elements present in the python list and divide the returned sum by the total count to get the average or mean.
7.1 Example
Consider the list that holds 3 integers and return the average of these elements using for loop
# Consider the list of integers scores=[100,200,300] # Using for loop total=0 for i in scores: total=total+i print("Average Score: ",total/len(scores)) # Output: # Average Score: 200.0
8. Using a while loop
Inside the while loop, we will access each element using the index position and add each element to the total variable. The sum is stored in this variable. Each time we need to increment the iterator till it is less than the length of the list.
Now we will use the len() function to return the total number of elements present in the list and divide the returned sum by the total count returned by len() function.
8.1 Example
Consider the list that holds 3 integers and return the average of these elements using a while loop.
# Consider the list of integers scores=[100,200,300] # Using while loop total=0 i=0 while (i < len(scores)): total=total+scores[i] i=i+1 print("Average Score: ",total/len(scores)) # Output: # Average Score: 200.0
9. Conclusion
In this article, you have learned different ways to find the average of list elements in Python. We used the mean() from both the numpy and statistics module directly to return the average. And used reduce() to get the average by passing the lambda expression and operator.add() method as a mathematical expression.
Finally, we used for loop to find the average of all elements present in the list. Next, we saw how to use a while loop to find the average of elements in the list. After that, we used sum() method with len() to return the average of elements directly in the list.
You may also like reading:
Python – Find Average of values in a List
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 sum() and len() functions to compute the mean of a list. Alternatively, you can also use methods defined in libraries such as statistics , numpy , etc. to get the average of a list of values.
📚 Discover Online Data Science Courses & Programs (Enroll for Free)
Introductory ⭐
Intermediate ⭐⭐⭐
🔎 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.
Let’s look at the above-mentioned methods with the help of examples.
1. Mean of a list using sum() and len()
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 len() function.
# 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.
2. Using statistics library
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 statistics.mean() function.
import statistics # create a list ls = [1,2,3,4] # average of list statistics.mean(ls)
We get the same result as above.
For more on the statistics library, refer to its documentation.
3. Using numpy library
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)
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.
Author
Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts
Data Science Parichay is an educational website offering easy-to-understand tutorials on topics in Data Science with the help of clear and fun examples.