Tutorials  /  Python

Calculating Averages Made Easy: An Overview of 5 Python Methods

Ccentron Redaktion · May 2024 ·3 min read ·Python, Tutorial

The Python ´mean()´ Function

The simplest method to calculate the average of a list is to use the statistics.mean() function. This function is available in Python 3 within the "statistics" module and accepts a list, tuple, or dataset containing numeric values as input.

Here is an example:

Python
from statistics import mean 

inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88] 
list_avg = mean(inp_lst) 

print("Average value of the list:\n") 
print(list_avg) 
print("Average value of the list rounded to 3 decimal places:\n")
print(round(list_avg,3))
VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

Using the Python ´sum()´ Function

Another method to calculate the average is to use the statistics.sum() function and the length of the list.

Here is an example:

Python
from statistics import mean 

inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]

sum_lst = sum(inp_lst)

lst_avg = sum_lst / len(inp_lst)
print("Average value of the list:\n") 
print(lst_avg) 
print("Average value of the list rounded to 3 decimal places:\n")
print(round(lst_avg,3))

Using Python ´reduce()´ and ´lambda()´

Another method to calculate the average of a list is to use the Python reduce() function in combination with the lambda() function.

Here is an example:

Python
from functools import reduce 

inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]

lst_len= len(inp_lst)

lst_avg = reduce(lambda x, y: x + y, inp_lst) / lst_len 
print("Average value of the list:\n") 
print(lst_avg) 
print("Average value of the list rounded to 3 decimal places:\n")
print(round(lst_avg,3))

Using the Python ´operator.add()´ Function

Another way to calculate the average using the operator module is to use the operator.add() function.

Here is an example:

Python
from functools import reduce 
import operator

inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]

lst_len = len(inp_lst)

lst_avg = reduce(operator.add, inp_lst) / lst_len 
print("Average value of the list:\n") 
print(lst_avg) 
print("Average value of the list rounded to 3 decimal places:\n")
print(round(lst_avg,3))

Using the NumPy ´average()´ Method

Finally, Python's NumPy module provides a built-in numpy.average() method to calculate the average/mean of the data items present in the dataset or list.

Here is an example:

Python
import numpy

inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]

lst_avg = numpy.average(inp_lst)
print("Average value of the list:\n") 
print(lst_avg) 
print("Average value of the list rounded to 3 decimal places:\n")
print(round(lst_avg,3))

Conclusion

Python provides multiple methods for calculating the average, each with its own advantages depending on the use case. While statistics.mean() is ideal for simple calculations, numpy.average() is particularly efficient for handling large datasets. The choice of the appropriate method depends on your specific requirements.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren