The basic way to calculate the average is by the sum of all elements divide by a number of elements. In this post, we are going to understand 7 ways to find the average of Python List with examples by using some built-in Function In Python.
These are many ways that we are using to find AVERAGE of a List Python
- using Loop
- Using sum() and len() function
- Using Python statistic sum() function
- Using Python reduce() and Lamda function
- Numpy mean() function
- Python operator.add() function
1. Loop to Find average of Python list
Step to find average of Python List
- We will loop through all elements of the list until exhausted.
- On every iteration, we will add the next list element in the sum variable.
- To find numbers of elements in the list will use the list len() function.
- Finally, Find the average by using this formula: sum/length.
Program to find the average of Python List
numlist = [50, 30, 40, 60, 72,80]
length = len(numlist)
sum = 0
for item in numlist:
sum = sum+item
list_averge = sum / len(numlist)
print("Average of list values : ", round(list_averge,2))
Output
Average of list elements : 55.33
2. Using sum() and len() function
In this example, we will make use of the sum() function to calculate the sum of all elements in the list and len() to calculate the length.
We will divide the sum() of the list elements by the number of elements in the list to calculate the average.
Finally rounding off of the result to two decimal using the round() function. Let us understand with the help of examples how to use them.
Program to find the average of Python List
numlist = [50, 30, 40, 60, 72,80]
list_avergae = sum(numlist)/len(numlist)
print("Average of list elements : ", round(list_avergae,2))
The output will be the average of list elements
Average of list elements : 55.33
3. Using statistic module mean() function
The statistics module in Python 3 has built-in statistics.mean() function which is used to find the mean or an average of list items.
Finally, rounding off of the result to two decimal places using Python round() function.
Program Example
from statistics import mean
numlist = [50, 30, 40, 60, 72,80]
list_averge = mean(numlist)
print("Average of list values : ", round(list_averge,2))
Output
Average of list values : 55.33
4. Using Python reduce() and Lambda function
In this code example, we are using the functools module‘s reduce() method along with the lambada function to find the average of python list. Let us understand how to accomplish this.
Program Example
from functools import reduce
numlist = [50, 30, 40, 60, 72,80]
len= len(numlist)
list_averge = reduce(lambda a, b: a + b, numlist) /len
print("Average of list values : ", round(list_averge,2))
Output
Average of list values : 55.33
5. NumPy mean() function
In this example, By using the NumPy module mean() function we can find the average of list items by passing list as an argument to mean() function.
To use Numpy module first need to import it using import numpy as np code.
import numpy as np
numlist = [50, 30, 40, 60, 72,80]
list_averge = np.mean(numlist)
print("Average of list values : ", round(list_averge,2))
Output
Average of list values : 55.33
6. Using Pandas series.mean()
The pandas series mean() function to can be use to find the average of the Python list. We pass the input list to pandas. series().mean() function and it will return the average of Python list . Let us understand with an example.
Program Example
import pandas as pd
numlist = [50, 30, 40, 60, 72,80]
list_averge = pd.Series(numlist).mean()
print("Average of list values : ", round(list_averge,2))
Output
Average of list values : 55.33
7. Using Python operator.add() function
In this example, we will use operator.add() method of operator module along with funtools module reduce() method to find the average of list elements.
Program Example
from functools import reduce
import operator
numlist = [50, 30, 40, 60, 72,80]
length = len(numlist)
list_averge = reduce(operator.add, numlist) /length
print("Average of list values : ", round(list_averge,2))
Output
Average of list values : 55.33
Summary:
We have explored 7 ways to find the average of Python List with code example that includes using Loop,sum() and len() function,statistic sum(), reduce() and Lamda function,Numpy mean(),Python operator.add().