Python »How to find the average of a list in Python

Average (arithmetic mean) is a mathematical function that is calculated by adding numeric values ​​in a list and dividing them by the number of numbers in the list. Python provides several built-in math functions; hence, it provides various ways to calculate the average of a list.

This article will discuss various approaches to get the average of lists in Python on Linux system. Here is a list of the methods mentioned below:

  • Finding the mean of a list using the sum () and len () functions.
  • Finding the mean value of a list using the mean () function by importing the statistics module.
  • Finding the mean of a list from the mean () function by importing the numpy module.
  • Finding the average of a list from the reduce () function by importing the functools and lambda () functions.

Requirements

Any version of Python that will be installed on your Linux system, python3 is preinstalled on the latest Ubuntu.

Creating a Python file

To write Python code in Ubuntu, you must create a Python file with the extension “.py”, create a file “python_file.py” using the command below:

$ 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 for finding the mean of a list in Python

The general syntax for finding an average over a list is mentioned below:

среднее = [ сумма значений в списке ]/[ количество значений в списке ]

Follow any of the below methods to help you find the average of a list of numbers in python:

How to find the average of a list using the sum () and len () functions

The first way to find the average is with the “functions sum () and len ()”. The sum () function calculates the sum of all the values ​​in a numeric list, and the len () function abbreviated to length gives the number of values ​​in the list. Following is the syntax for calculating the average using the sum () and len () functions:

average= sum(list)/len(list)

The list contains the numerical values ​​of which the average is to be calculated. Write the following code in “python_file.py” to find the average over the list:

print("Поиск среднего с помощью функций sum() и len()")

list_values={1,3,5,7,9,11}

average=sum(list_values)/len(list_values)

print("Среднее значение list_values равно: ",average)

To get the desired result, execute the code written in “python_file.py” with the command below:

$ python3 python_file.py

How to find the mean over the mean () function from the statistics module

Another way to compute the mean of a list is to use the mean () function by importing the statistics module. The mean function takes a numeric list as an argument and performs the function of the mean in the list, but we cannot use this function without importing the statistics module, its syntax is below:

import statistics

average= mean(list)

where:

  • statistics: built-in module in python, to execute middle function import this module
  • list: contains the numeric values ​​whose average is to be calculated.

Write the below code in python_file.py to calculate the mean of a list using the mean function:

import statistics

print ("Поиск среднего значения с помощью функции mean() в модуле статистики")

list_values={1,3,5,7,9,11}

average=statistics.mean(list_values)

print("Среднее значение list_values равно: ",average)

To get the desired result, execute the code written in python_file.py with the command below:

$ python3 python_file. ру

How to find mean using mean function from numpy module

We can compute the mean of a list using the mean () function from the numpy module. The numpy module is a popular choice for working with large multidimensional arrays. It also has a ton of math functions that can be applied to arrays to accomplish a variety of tasks. One of the most important is the mean () method, which returns the mean of the provided list, but that list must be of type array.

To use the numpy module to calculate the average, you first need to install it with the command below:

$ pip install numpy

Following is the syntax for calculating the average using the “numpy” module:

import numpy

average=numpy.mean(list)

where:

  • numpy: the library to import in order to use the average function to calculate the average.
  • list: contains the numeric values ​​whose average is to be calculated.

Write the following code in python_file.py to find the mean of a list using the mean function from the numpy module:

import numpy

print ( "Поиск среднего значения с помощью функции mean() в модуле numpy" )

list_values = [ 1 , 3 , 5 , 7 , 9 , 11 ]

average = numpy. mean ( list_values )

print ( "Среднее значение" list_values ":" , среднее )

To get the desired result, execute the code written in “python_file.py” with the command below:

$ python3 python_file.py

How to find the mean using reduction and lambda functions

To find the average of a list using reduce () and a 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 over the list.

import functools

average=functools.reduce(lambda i,j : i+j,list)/len(list)

where:

  • “I, j”: lambda arguments.
  • i + j: expression for calculating the sum of the list using lambda arguments.
  • len (): gives the number of values ​​in the list.

Write the following code in python_file.py to find the average of a list using the lambda () and reduce () functions from the functools module:

import functools

print("Поиск среднего с помощью функций lambda() и reduce()")

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("Среднее значение list_values ​​равно:" , среднее)

To get the desired result, execute the code written in “python_file.py” with the command below:

$ python3 python_file.py

Conclusion

Python provides many built-in modules for using math functions. The method is used to calculate the average of the list. Article 4 describes methods with examples for calculating the average of a list; using the sum () and len () functions, using the mean () function from the statistics module, using the mean () function from the numpy module and using the lambda () and reduce () functions. After reading this article, you will learn various methods for finding the average in Python.

Related Posts