The 5 ways to find the mean of a list in Python

Atualizado em 2026-10-28 14:01:19 20 visitas

What is the mean of a list?

Before diving into the ways to find the mean of a list in Python, it is important to understand what the mean is. It is the central numerical value of a data set obtained by summing all values and dividing the result by the number of elements in the list.

The mean is an important concept in statistics and in many practical applications in areas such as data science, engineering, and economics. By calculating the mean, we can obtain a deeper understanding of how the data is distributed and how a population or sample behaves.

In Python, there are many different ways to find the mean of a list, from using built-in functions to creating your own custom functions. Once this has been clarified, we will present 5 ways to find it.

Method 1: Using the sum() and len() functions

The sum() function takes a list of numbers as an argument and returns the sum of all of them. On the other hand, the len() function returns the quantity of elements in a list. Using these two functions together, we can easily calculate the mean of a list in Python.

Here is a code example:

my_list = [2, 4, 6, 8, 10]
mean = sum(my_list) / len(my_list)
print('The mean of the list is:', mean)

In this example, we create a list of numbers and then use the sum() function to sum all the elements of the list. Then, we divide the result of the sum by the quantity of elements in the list using the len() function. Finally, we print the result as the value of the mean.

info With just two built-in Python functions, you can easily calculate the mean of any list of numbers you have.

Method 2: Using the mean() function from NumPy

NumPy is a Python library used for numerical and scientific calculation. One of its built-in functions is mean(), which is used to find the mean of a set of numbers.

To do this, you would use, for example, this code:

import numpy as np

my_list = [2, 4, 6, 8, 10]
mean = np.mean(my_list)
print('The mean of the list is:', mean)

In this instance, we import NumPy with the syntax import numpy as np. Then, we create a list of numbers and use NumPy's mean() function to calculate the mean of the list. Finally, we print the result as the value of the mean.

info With the NumPy library, you can perform complex numerical calculations in a fast and efficient way in Python.

Method 3: Using the statistics.mean() function from the statistics library

The statistics library is a Python library that includes several useful statistical functions. One of these functions is mean(), which is used to calculate the mean of a list of numbers.

Here is a code example:

import statistics

my_list = [2, 4, 6, 8, 10]
mean = statistics.mean(my_list)
print('The mean of the list is:', mean)

In this case, we import the statistics library with the syntax import statistics. Then, we create a list of numbers and use the mean() function from statistics to calculate the mean of the list. Finally, we print the result to obtain the value of the mean.

info The statistics library is very useful for performing statistical and mathematical calculations in Python in a simple way.

Method 4: Using a for loop and summing the elements of the list

Another way to calculate the mean of a list in Python is by using a for loop to sum all the elements of the list and then divide that sum by the quantity of elements in the list.

A code example you can try:

my_list = [2, 4, 6, 8, 10]
total_sum = 0
for num in my_list:
    total_sum += num
mean = total_sum / len(my_list)
print('The mean of the list is:', mean)

Here we create a list of numbers and use a for loop to sum all the elements of the list. Then, we divide that sum by the quantity of elements in the list to obtain the mean. Finally, we print the result as the value of the mean.

info This way of calculating the mean is a bit more complex than the previous ones, but it is very useful when you do not have access to external libraries or simply prefer to use simpler and more direct code in Python.

Method 5: Using the reduce() function from the functools library

The reduce() function from the functools library allows us to apply an operation to all elements of a list in an accumulative way. To calculate the mean of a list using reduce(), first we have to import the functools library and the reduce() function. Then, we use reduce() to sum all elements of the list and divide that sum by the quantity of elements in the list.

Here is a code example:

from functools import reduce

my_list = [2, 4, 6, 8, 10]
total_sum = reduce(lambda x, y: x + y, my_list)
mean = total_sum / len(my_list)
print('The mean of the list is:', mean)

In this example, we import the functools library and the reduce() function. Then, we create a list of numbers and use reduce() to sum all the elements of the list. Finally, we divide that sum by the quantity of elements in the list to obtain the mean.

info This way of calculating the mean is very useful when working with large amounts of data and a fast and efficient solution is needed.

Extra: Creating a custom function

As you read, in the end we not only present 5 ways to find the mean of a list in Python, but six. This option can be useful if you need to perform additional calculations or if you wish to customize the output of your function.

If you need to calculate the mean of a list on multiple occasions in your code, it may be useful to create a custom function to do it more easily and quickly. A function is a block of code that performs a specific task and can be called at any time.

Here is a code example:

def calculate_mean(my_list):
    total_sum = 0
    for num in my_list:
        total_sum += num
    mean = total_sum / len(my_list)
    return mean

my_list = [2, 4, 6, 8, 10]
mean = calculate_mean(my_list)
print('The mean of the list is:', mean)

In this example, we create a custom function called calculate_mean that takes a list as a parameter. Inside the function, we create a variable called total_sum that starts at zero and use a for loop to iterate over each number in the list and sum them. Then, we divide the total sum by the length of the list to obtain the mean and return it as a result.

To use this function, we simply pass our list as an argument and store the result in a variable called mean.

success Ready! You now know six different ways to find the mean of a list in Python. You can now make the most of your tasks and/or projects.

Conclusions

We hope that this tutorial has been of great help to you. Calculating the mean of a list is a common task in data analysis and Python programming, and these five options will allow you to do it in a fast and efficient way. Additionally, remember that you can always customize your code and create your own function so that it adapts to your specific needs.

Don't be discouraged if you don't feel comfortable with Python yet. Programming can be challenging, but it can also be exciting and quite rewarding. With practice and perseverance, you will be able to master any task in Python that comes your way.