In this Article different approaches will be discussed to get the average of lists in Python on Linux System. Here is the list of methods mentioned below:
- Finding the average of the list by using sum() and len() Function.
- Finding the average of the list from the mean() function by importing the statistic module.
- Finding the average of the list from the mean() function by importing the numpy module.
- Finding the average of the list from reduce() by importing functools and lambda() functions.
Requirements
Any Python version to be installed on your Linux System, python3 is preinstalled on the latestS Ubuntu version.
Creating a Python file
To code in python on Ubuntu, you have to create a Python file with “.py” extension, create “python_file.py” file by below mentioned command:
Important note:
- # is used to write comments (explanatory statements), they are not executed during program execution.
- Press Ctrl+s to save the newly created python file and Ctrl+x to exit the file.
Methods to Find Average of List in Python
General syntax to find average of list is mentioned below:
Follow any of the method below which you find easier to find average of list of numbers in python:
How to find average of list using sum() and len() functions
First way to find the average is with the help of “sum() and len() functions”. The sum() function calculates the sum of all values in the numeric list and len() function short for length gives the count of values in the list. Below mentioned is syntax to calculate average using sum() and len() function:
The list contains the numeric values whose average is to be calculated. Write the below mentioned code in the “python_file.py” to find the average of list:
list_values={1,3,5,7,9,11}
average=sum(list_values)/len(list_values)
print("The average of list_values is: ",average)
To get desired output, execute the code written in “python_file.py” by below mentioned command:
How to find average by mean() function from statistics module
Another way to calculate the average of the list is with the help of the mean() function by importing the statistics module. The mean function takes numeric list as an argument and perform average function on list but we cannot use this function without importing statistics module, below mentioned is its syntax:
average= mean(list)
statistics: built-in module in python, to perform mean function import this module
list : contains the numeric values whose average is to be calculated.
Write the below mentioned code in python_file.py to calculate average of list using mean function:
print("Finding average using mean() function in statistics module")
list_values={1,3,5,7,9,11}
average=statistics.mean(list_values)
print("The average of list_values is: ",average)
To get desired output, execute the code written in python_file.py by below mentioned command:
How to find average using mean function from numpy module
We can calculate the average of the list by mean() function from the “numpy” module. The numpy module is a popular choice for working with big multi-dimensional arrays. It also has a huge number of mathematical functions that may be applied to arrays to execute a variety of tasks. One of the most significant is the mean() method, which returns the average for the supplied list but that list must be of array type.
To use numpy module to calculate mean, first you need to install it by below mentioned command:
Below mentioned is the syntax to calculate average using “numpy” module:
average=numpy.mean(list)
numpy: library to be imported to use mean function to calculate average.
list: contains the numeric values whose average is to be calculated.
Write the below mentioned code in the “python_file.py” to find the average of list using mean function from numpy module:
print("Finding average using mean() function in numpy module")
list_values=[1,3,5,7,9,11]
average=numpy.mean(list_values)
print("The average of "list_values" is: ",average)
To get desired output, execute the code written in “python_file.py” by below mentioned command:
How to find average using reduce and lambda functions
To find the average of a list using reduce() and lambda, you need to import the functools module to use the reduce() function in Python. The lambda() function can be used to calculate the sum and the reduce() function can be used to iterate through the list.
average=functools.reduce(lambda i,j : i+j,list)/len(list)
“i,j”: are the arguments of lambda.
i+j: expression to calculate sum of list using arguments of lambda.
len(): gives the count of values in the list.
Write the below mentioned code in the “python_file.py” to find the average of list using lambda() and reduce() function from “functools” module:
print(“Finding average using lambda() and reduce() functions”)
def find_average(list_values):
average= functools.reduce(lambda i, j: i + j, list_values)/len(list_values)
return average
list_values={1,3,5,7,9,11}
average=find_average(list_values)
print("The average of list_values is: ",average)
To get desired output, execute the code written in “python_file.py” by below mentioned command:
Conclusion
Python provides many built-in modules to use mathematical functions. To calculate mean of list average method is used.In the Article 4 methods are explained with examples to calculate the average of list; by using sum() and len() function, by using mean() function from statistic module, by using mean() function from numpy module and by using lambda() and reduce() function. After going through this article, you learn different methods to find the average in Python language.