# Python Tutorial: How to Average a list in Python

## Introduction

Python is a popular programming language that is widely used in the field of data science, machine learning, and web development. One of the most common tasks in programming is to find the average (also known as mean) of a list of numbers. In this tutorial, we will learn how to calculate the average of a list in Python.

Calculating the average of a list is a simple task in Python, but it requires some basic knowledge of programming concepts such as loops and arithmetic operations. We will start by looking at the basic syntax for calculating the average of a list in Python. Then we will move on to some examples that demonstrate how this can be done using different approaches.

To follow along with this tutorial, you should have a basic understanding of Python programming and know how to create and manipulate lists. If you are new to Python or need a refresher, there are many resources available online that can help you get started with Python programming.

## What is a list in Python?

In Python, a list is a collection of elements that are ordered and mutable. This means that you can add, remove, or modify elements in a list after it has been created. Lists in Python are denoted by square brackets [ ] and each element in the list is separated by a comma.

For example, consider the following list:


my_list = [1, 2, 3, 4, 5]


This creates a list called my_list with five elements: 1, 2, 3, 4, and 5. You can access individual elements in the list using their index value. In Python, the first element of a list has an index value of 0.

For example:


print(my_list[0]) # Output: 1


This code will print the first element of my_list, which is 1.

Lists are very useful in Python as they allow you to store and manipulate large amounts of data efficiently. They can contain any type of data including integers, floats, strings or even other lists!

## How to calculate the average of a list in Python

Calculating the average of a list in Python is a common operation in data analysis and statistics. In Python, we can easily calculate the average of a list using built-in functions.

To calculate the average of a list, we first need to add up all the elements in the list and then divide by the number of elements. We can use the sum() function to add up all the elements in the list and the len() function to get the number of elements.

Here’s an example code snippet that demonstrates how to calculate the average of a list in Python:


numbers = [2, 4, 6, 8, 10]
average = sum(numbers) / len(numbers)
print("The average is:", average)


In this example, we have a list of numbers [2, 4, 6, 8, 10]. We use the sum() function to add up all the numbers in the list and store it in the variable sum. We then use the len() function to get the length of the list (which is equal to the number of elements) and divide sum by len(numbers) to get the average. Finally, we print out the result.

The output of this code will be:
The average is: 6.0

This means that the average of all numbers in our list is 6.0.

In summary, calculating the average of a list in Python is simple and can be achieved with just a few lines of code using built-in functions such as sum() and len().

## Method 1: Using the sum() function and len() function

In Python, there are several ways to find the average of a list. One of the simplest methods is by using the sum() function and len() function.

The sum() function returns the total sum of all elements in a list, while the len() function returns the total number of elements in a list. By dividing the sum of all elements by the total number of elements, we can find the average.

Let’s take a look at an example:


numbers = [2, 4, 6, 8, 10]
average = sum(numbers) / len(numbers)
print("The average is:", average)


In this example, we have a list of numbers containing 2, 4, 6, 8, and 10. We first use the sum() function to find the total sum of all elements in the list, which is equal to 30. We then use the len() function to find the total number of elements in the list, which is equal to 5. Finally, we divide the sum by the length to get our average value of 6.

This method is simple and easy to understand. However, it may not be suitable for very large lists as it requires calculating the sum of all elements in the list before finding the average.

In summary, using the sum() function and len() function is a straightforward way to find the average of a list in Python.

## Method 2: Using the statistics module

Python has a built-in module called statistics, which provides various statistical functions. One of these functions is the mean() function, which can be used to calculate the average of a list.

To use the mean() function, we first need to import the statistics module. We can do this using the import statement:


import statistics


Once we have imported the statistics module, we can use the mean() function to calculate the average of a list. Here’s an example:


import statistics

my_list = [1, 2, 3, 4, 5]
average = statistics.mean(my_list)

print("The average of the list is:", average)


In this example, we first create a list called my_list. We then call the mean() function from the statistics module, passing in our list as an argument. The mean() function returns the average of the list, which we store in a variable called average. Finally, we print out the value of average.

Note that if our list contains any non-numeric values (such as strings), calling the mean() function will result in a TypeError. Therefore, it’s important to ensure that our list only contains numeric values before calling the mean() function.

Overall, using the statistics module is a simple and effective way to calculate the average of a list in Python.

## Method 3: Using a for loop

Another way to calculate the average of a list in Python is by using a for loop. This approach is useful when you need to perform additional operations on each element of the list before calculating the average.

Here’s an example code snippet that demonstrates how to use a for loop to calculate the average of a list:


def avg_list(lst):
sum = 0
for num in lst:
sum += num
return sum / len(lst)


In this example, we first initialize a variable called sum to 0. Then, we iterate over each element of the list using a for loop and add it to the sum variable. Finally, we divide the sum by the length of the list to get the average.

Let’s test this function with an example:


>>> lst = [1, 2, 3, 4, 5]
>>> avg = avg_list(lst)
>>> print(avg)
3.0


As expected, the average of [1, 2, 3, 4, 5] is 3.0.

Using a for loop can also be useful when you need to filter out certain elements from the list before calculating the average. For example, let’s say you have a list of grades and you want to calculate the average excluding any grades below 60:


def avg_above_60(lst):
sum = 0
count = 0
count += 1
return sum / count if count > 0 else None


In this example, we first initialize two variables – sum and count – to 0. Then, we iterate over each element of the list using a for loop and check if the grade is above or equal to 60. If it is, we add it to the sum variable and increment the count variable. Finally, we divide the sum by the count to get the average.

Let’s test this function with an example:


>>> grades = [80, 70, 55, 90, 65]
>>> print(avg)
81.25


As expected, the average of [80, 70, 90, 65] is 81.25.

## Conclusion

In this tutorial, we learned how to calculate the average of a list in Python using different methods. We started by using a for loop to iterate over the list and summing up all the elements. Then, we divided the sum by the length of the list to get the average.

We also explored how to use the built-in function sum() along with len() to calculate the average of a list in a more concise way. Finally, we saw how to use the third-party library NumPy’s mean() function to get the average of a list.

It’s important to choose the right method based on your specific use case. If you’re working with small lists, using a for loop or built-in functions might be enough. However, if you’re working with larger datasets, NumPy’s mean() function can provide significant performance improvements.

Overall, calculating the average of a list is a fundamental operation in data analysis and statistics, and having these techniques in your Python toolbox can be incredibly useful.
Interested in learning more? Check out our Introduction to Python course!

Your FREE Guide to Become a Data Scientist

Discover the path to becoming a data scientist with our comprehensive FREE guide! Unlock your potential in this in-demand field and access valuable resources to kickstart your journey.

##### Pierian Training
Pierian Training is a leading provider of high-quality technology training, with a focus on data science and cloud computing. Pierian Training offers live instructor-led training, self-paced online video courses, and private group and cohort training programs to support enterprises looking to upskill their employees.

## You May Also Like

### Guide to NLTK – Natural Language Toolkit for Python

Introduction Natural Language Processing (NLP) lies at the heart of countless applications we use every day, from voice assistants to spam filters and machine translation. It allows machines to understand, interpret, and generate human language, bridging the gap between humans and computers. Within the vast landscape of NLP tools and techniques, the Natural Language Toolkit […]

### GridSearchCV with Scikit-Learn and Python

Introduction In the world of machine learning, finding the optimal set of hyperparameters for a model can significantly impact its performance and accuracy. However, searching through all possible combinations manually can be an incredibly time-consuming and error-prone process. This is where GridSearchCV, a powerful tool provided by Scikit-Learn library in Python, comes to the rescue. […]

### Plotting Time Series in Python: A Complete Guide

Introduction Time series data is a type of data that is collected over time at regular intervals. It can be used to analyze trends, patterns, and behaviors over time. In order to effectively analyze time series data, it is important to visualize it in a way that is easy to understand. This is where plotting […]