Get the average of List values in Python
Python is a versatile language that can be used for a wide variety of programming tasks. One such task is working with lists. In this article, we'll take a look at how to get the average of a list in Python.
There are a few different ways to calculate the average of a list in Python. The most straightforward way is to use the built-in sum() and len() functions. sum() will add up all of the values in the list, and len() will give us the number of items in the list. We can then divide the sum by the length to get the average.
Another way to calculate the average is to use the mean() function from the statistics module. This method is a bit more concise and is the recommended way if you're working with large lists.
Once you have the average, you might want to round it to a specific number of decimal places. To do this, you can use the round() function.
We'll walk through each of these methods with some examples so you can see how they work.
Solution 1: Using mean() function of statistics module
In Python, the mean() function of the statistics module can be used to calculate the average of a list of values. The mean() function takes a list of values as an argument and returns the average of the values in the list.
Below is the syntax to find the average of List numbers in Python.
mean(List l)
Code example
from statistics import mean
# create a list
numbers = [30, 50, 90, 70, 20, 10]
result_avg = mean(numbers)
print("Average is: ", result_avg)
Output
Average is: 45
The code is first importing the mean function from the statistics module. It is then creating a list of numbers and using the mean() function to find the average of the numbers in the list. Finally, it is printing the average.
Solution 2: Get the average of List values using sum() and len() function
Python has many built-in functions that allow you to perform various operations on lists. Two such functions are the sum() and len() functions. The sum() function returns the sum of all the values in a list, while the len() function returns the number of values in a list. To get the average of all the values in a list, you can use the sum() and len() functions together.
Syntax
sum(List l) / len(List l)
Code example
# create a list
numbers = [30, 50, 90, 70, 20, 10]
# find the average using sum() and len() functions
result_avg =sum(numbers) / len(numbers)
print("Average is: ", result_avg)
Output
Average is: 45.0
This code example finds the average of a list of numbers. The sum() function calculates the sum of the numbers in the list, and the len() function calculates the number of items in the list. The result is divided by len() to find the average.
Solution 3: Using numpy.mean() function
Python's NumPy library provides a numpy.mean() function that calculates the average of a list of values. This function takes a list as an argument and returns the average of the list's values as a float. So if you are using the numpy library in your project, you can find the average of list values using its numpy.mean() function.
Below is the syntax:
numpy.mean(List)
Code example
import numpy as np
numbers = [30, 50, 90, 70, 20, 10]
# find the average using np.mean() function
result = np.mean(numbers)
print("Average is: ", result)
Output
Average is: 45.0
In the above code example:
- Import the `numpy` library as `np`.
- Create a list of numbers.
- Use the `np.mean()` function to calculate the average of the numbers in the list.
- Print the result.
Solution 4: Using reduce() and lambda functions
In order to get the average of a list of values using reduce() and lambda functions in python, we first need to import the reduce() function from the functools module. Then, we can create a lambda function that takes in two parameters, the first being the accumulator and the second being the current value. Within the lambda function, we can then return the accumulator + the current value. Finally, we can call the reduce() function on our list of values and pass in our lambda function as the first argument.
from functools import reduce
numbers = [30, 50, 90, 70, 20, 10]
# find the average using reduce() and lambda functions
result = reduce(lambda a, b: a + b, numbers) / len(numbers)
print("Average is: ", result)
Output
Average is: 45.0
This code calculates the average of a list of numbers using the reduce() and lambda functions. The reduce() function applies the lambda function to the first two elements of the list, then applies the lambda function to the result of that operation and the next element in the list, and so on. The lambda function simply adds the two operands and divides them by the length of the list to find the average.
Solution 5: Get the average of List values using Python For Loop
Here, we will take a list of numbers as input and print the average of the list using a for loop in Python. We will iterate through the items of the list and then add them in each iteration. Then to calculate the average, we will divide the sum by the length of the string.
numbers = [30, 50, 90, 70, 20, 10]
num_sum = 0
for num in numbers:
num_sum += num
average = num_sum / len(numbers)
print("Calculated average is: ", average)
Output
Calculated average is: 45.0
The code is an example of how to calculate the average of a list of numbers. The list of numbers is stored in the variable "numbers". The variable "num_sum" is used to store the sum of all the numbers in the list. The "For" loop iterates over each number in the list, and adds it to the variable "num_sum". Finally, the variable "average" is calculated by dividing the variable "num_sum" by the length of the list of "numbers".
Solution 6: Using pandas.Series(List).mean() function
In order to find the average of a list of values, we can use the pandas.Series(List).mean() function. This function will take in a list as an input and return the average of the values in the list.
Syntax
import pandas as pd
pd.Series(List).mean()
Code example
import pandas as pd
numbers = [30, 50, 90, 70, 20, 10]
result_average = pd.Series(numbers).mean()
print("Calculated average is: ", result_average)
Output
Calculated average is: 45.0
The code is importing the "pandas" library as pd. Then it is creating a list called numbers. After that, it is using the Series function in pandas to take the mean of the numbers in the list. Finally, it is printing the result.
- Get all values by key name from a list of dictionaries Python
- Get dictionary values as a list in python
- Get integer only values from a list in Python
- Pandas - Get index values of a DataFrame as a List
- Get column values as list in Pandas DataFrame
- Convert all string values in a list to integers using python
- Get all values from a dictionary in Python