Python

# How to find the average of a list in Python

Average (Arithmetic mean) is a mathematical function which is calculated by adding the numeric values in the list and dividing them by the count of numbers of the list. Python provides several built-in mathematical functions; consequently it provides different ways to calculate the average of a list.

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:

\$ nano python_file.py

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:

average=[sum of values in list]/[number of values in list]

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:

average= sum(list)/len(list)

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:

print("Finding average using sum() and len() functions")

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:

\$ python3 python_file.py

## 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:

import statistics

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:

import statistics

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:

\$ python3 python_file.py

## 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:

\$ pip install numpy

Below mentioned is the syntax to calculate average using “numpy” module:

import numpy

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:

import numpy

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:

\$ python3 python_file.py

## 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.

import functools

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:

import functools

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:

\$ python3 python_file.py

## 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.

#### Alishba Iftikhar

I am currently an undergraduate student in my 1st year. I am an internee author with Linuxhint and loved learning the art of technical content writing from senior authors. I am looking forward to opting my career as a full time Linux writer after I graduate.