While working with Python, we can have a problem in which we need to find mean of a list cumulative with float elements. This problem is common in Data Science domain. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using loop + formula
The simpler manner to approach this problem is to employ the formula for finding mean and perform using loop shorthands. This is the most basic approach to solve this problem.
# Python3 code to demonstrate working of # Average of Float Numbers # using loop + formula import math # initialize list test_list = [6.1, 7.2, 3.3, 9.4, 10.6, 15.7] # printing original list print("The original list is : " + str(test_list)) # Average of Float Numbers # using loop + formula sum = 0for ele in test_list: sum += ele res = sum / len(test_list) # printing result print("The mean of float list elements is : " + str(res)) |
The original list is : [6.1, 7.2, 3.3, 9.4, 10.6, 15.7] The mean of float list elements is : 8.716666666666667
Method #2 : Using statistics.fmean()
This task can also be performed using inbuilt function of fmean(). This is new in Python versions >= 3.8.
# Python3 code to demonstrate working of # Average of Float Numbers # using statistics.fmean() import statistics # initialize list test_list = [6.1, 7.2, 3.3, 9.4, 10.6, 15.7] # printing original list print("The original list is : " + str(test_list)) # Average of Float Numbers # using statistics.fmean() res = statistics.fmean(test_list) # printing result print("The mean of float list elements is : " + str(res)) |
The original list is : [6.1, 7.2, 3.3, 9.4, 10.6, 15.7] The mean of float list elements is : 8.716666666666667
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Convert String float to float list in Python
- Python - Convert Float String List to Float Values
- Python | Convert Joint Float string to Numbers
- Average of Cubes of first N natural numbers
- Find average of a list in python
- Python | Average of two lists
- Python | Average of each n-length consecutive segment in a list
- Python | Replace NaN values with average of columns
- Numpy MaskedArray.average() function | Python
- Python | Average String length in list
- Python | Record elements Average in List
- Python | Column Average in Record List
- Python - Rear elements Average in List
- Find the average of an unknown number of inputs in Python
- Python - Get Random Range Average
- Calculate the average, variance and standard deviation in Python using NumPy
- Find sum and average of List in Python
- Python - Average of digit greater than K
- Python - Average digits count in a List
- How to Compute the Average of a Column of a MySQL Table Using Python?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

