# How to find the Mean of a List in Python [6 Methods]

Do you want to find the mean of a list storing numbers in it? In this Python tutorial, I will tell you how to find the mean of a list in Python using different methods with some illustrative examples.

To find the mean of a list in Python, we can use various methods such as calculating it manually with a for loop, employing a combination of sum() and len() functions, utilizing list comprehensions for a more Pythonic approach, or leveraging built-in functions like statistics.mean(). For more advanced calculations, we might apply the reduce function with a lambda, or use the NumPy library for efficient computation.

Table of Contents

## Methods to find the mean of a list in Python

There are six different methods to find the mean of a list in Python:

• The sum() and len() Functions
• List Comprehension
• A For Loop
• The Statistics Library
• The reduce() with a lambda Function
• The NumPy Library

Let’s see them one by one using some illustrative examples:

Before diving into Python code, it’s essential to understand what the mean is. The mean is calculated by summing up all the values in a dataset and then dividing this total by the count of the values.

Formula:

### 1. Find the mean of a list in Python using the sum() and len() function

Python provides built-in functions sum() and len() that can simplify the mean calculation. The sum() function will find the sum of all the values in the Python list and the len() function will return us the total number of values in the Python list.

Let’s take an example:

``````annual_incomes = [50000, 60000, 75000, 80000, 45000]
mean_income = sum(annual_incomes) / len(annual_incomes)
print("Average Income:", mean_income)``````

Output:

``Average Income: 62000.0``

The ensuing image captures the results produced by executing the code in PyCharm.

### 2. Python Mean of List using List Comprehension

For a more compact approach, we can use list comprehension with the sum() and the len() function to find the mean of a list in Python. For instance:

``````monthly_rents = [1200, 1600, 1100, 800, 1400, 1700]
mean_rent = sum([rent for rent in monthly_rents]) / len(monthly_rents)
print("Average Monthly Rent:", mean_rent)``````

Output:

``Average Monthly Rent: 1300.0``

Below is an image displaying the results of the code execution in the PyCharm environment.

### 3. Find the Mean of a List of Numbers in Python using a For-Loop

The most basic method to calculate the mean of a list in Python is by using a for loop to iterate through the list and sum up the values. For example:

``````def calculate_mean(stock_prices):
total_sum = 0
for number in stock_prices:
total_sum += number
return total_sum / len(stock_prices)

weekly_prices = [150, 152, 149, 153, 151]
average_price = calculate_mean(weekly_prices)
print("Average Stock Price:", average_price)``````

Output:

``Average Stock Price: 151.0``

The screenshot below showcases the output generated by running the code within the PyCharm editor.

### 4. Python list mean function in the statistic library

The mean() function from the statistics module for a direct and simple way to calculate the mean of a list in Python. For instance:

``````import statistics

temperatures = [72, 74, 68, 70, 71, 75, 73, 76, 84, 77]
mean_temperature = statistics.mean(temperatures)
print("Mean Temperature:", mean_temperature)``````

Output:

``Mean Temperature: 74``

The output from running the code in PyCharm is visually represented in the screenshot below.

### 5. Python program to calculate the mean of list numbers using reduce() with a lambda function

The reduce() function, combined with a lambda function, can also be used to find the mean of a list in Python. reduce() is part of the functools module and is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along.

``````from functools import reduce
daily_steps = [3000, 7000, 5000, 8000, 6000]
total_sum = reduce(lambda a, b: a + b, daily_steps)
mean_steps = total_sum / len(daily_steps) if daily_steps else 0
print("Average Daily Steps:", mean_steps)``````

Output:

``Average Daily Steps: 5800.0``

Displayed below is a screenshot capturing the outcome of the code execution in the PyCharm editor.

### 6. Find mean of numbers list in Python using the NumPy library

The mean() function from the NumPy library calculates the arithmetic mean in Python, which is the sum of the elements along an axis divided by the number of elements.

This is how it is used to find the mean of a list in Python:

``````import numpy as np

weekly_prices = [200, 202, 199, 203, 201]
average_price = np.mean(weekly_prices)
print("Average Stock Price:", average_price)``````

Output:

``Average Stock Price: 201.0``

The following screenshot illustrates the results obtained from executing the code in the PyCharm editor.

## Conclusion

Here, I have explained how to find the mean of a list in Python using six different methods such as the sum() with the len() function, built-in functions like statistics.mean(), the elegance of list comprehensions, the control of a for loop, the functional approach using reduce and lambda, or the power and speed of the NumPy library.

Choosing the appropriate method depends on your specific context, such as the size of your dataset and the level of performance optimization needed.

You may also like to read: