In previous articles, we studied about 2d lists in python, how to concatenate multiple lists, how to shuffle lists, how to reverse a list, and so much more. In this article, we will know how to find the average list in python.
The average is the number that represents the central tendency in a group of numbers. For example, if we have some numbers like 3 4 5 6 7. The average number is the sum of all the numbers divided by the total number of numbers.
3+4+5+6+7=25
25/5=5.
So, 5 is the average of these numbers.
In python, we have multiple ways to find the average of elements of the list. We can use built-in functions as well as by creating our functions. So, let us learn each method one by one.
Contents
- Finding Average of lists in python using Loop
- Finding the average of lists in Python using the Numpy Library
- Using statistics Library to Find the average of lists in python
- Using reduce function to find the average of lists in Python
- Finding Average of list Without Python Lambda Function
- Finding Average of the list With Python Lambda Function
- Finding the average of the list in python containing strings
- Calculating The Average of Data Frame in Python
- Must Read
- Conclusion
Finding Average of lists in python using Loop
We can also use the sum[] method directly-
Finding the average of lists in Python using the Numpy Library
For using numpy library we need to install it using pip install numpy
Using statistics Library to Find the average of lists in python
Using reduce function to find the average of lists in Python
We can also find the mean or average of the list using the reduce[] function of the functools library. In reduce function, we need to pass two parameters. The first one is the function that we want to perform on the two elements, and the second one is the list of items. Reduce function will automatically take two elements from the list. If we have a small function, it is a good practice to pass a lambda function as the first parameter.
Finding Average of list Without Python Lambda Function
Another way is by using the add function of the operator module
Finding Average of the list With Python Lambda Function
Finding the average of the list in python containing strings
Suppose we have a list, containing numbers but in the form of strings, like this-
list_strings = [1,2,3,4,5,6,7]
We want to calculate the average of this string, but none of the methods we discussed can be used to calculate the average. We have to convert this list into integer type and then calculate the average of the list after any of the methods discussed.
For example-
A better way to convert the list of strings into an integer is by using the map function-
Note- We cannot find the average if the elements are like this one, two, three, four.
Calculating The Average of Data Frame in Python
Must Read
- How to Convert String to Lowercase in
- How to Calculate Square Root
- User Input | Input [] Function | Keyboard Input
- Best Book to Learn Python
Conclusion
We have studied so many methods to find the average of the list in python. Python offers a variety of built-in functions like statistics.mean[] and numpy.mean[]. We can also find the average of a list containing numbers as a string.
Try to run the programs on your side and let us know if you have any queries.
Happy Coding!