![]() |
|
[Tut] Python List Average - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python List Average (/thread-94483.html) |
[Tut] Python List Average - xSicKxBot - 04-10-2020 Python List Average Don’t Be Mean, Be Median. This article shows you how to calculate the average of a given list of numerical inputs in Python. In case you’ve attended your last statistics course a few years ago, let’s quickly recap the definition of the average: sum over all values and divide them by the number of values. So, how to calculate the average of a given list in Python? Python 3.x doesn’t have a built-in method to calculate the average. Instead, simply divide the sum of list values through the number of list elements using the two built-in functions Code: sum()Code: len()Code: listCode: sum(list)/len(list)Here’s a short example that calculates the average income of income data $80000, $90000, and $100000: Code: income = [80000, 90000, 100000]
average = sum(income) / len(income)
print(average)
# 90000.0You can see that the return value is of type float, even though the list data is of type integer. The reason is that the default division operator in Python performs floating point arithmetic, even if you divide two integers. Puzzle: Try to modify the elements in the list Code: incomeCode: <iframe height="700px" width="100%" src="https://repl.it/@finxter/averagepython?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>If you cannot see the interactive shell, here’s the non-interactive version: Code: # Define the list data
income = [80000, 90000, 100000] # Calculate the average as the sum divided
# by the length of the list (float division)
average = sum(income) / len(income) # Print the result to the shell
print(average) # Puzzle: modify the income list so that
# the result is 80000.0This is the absolute minimum you need to know about calculating basic statistics such as the average in Python. But there’s far more to it and studying the other ways and alternatives will actually make you a better coder. So, let’s dive into some related questions and topics you may want to learn! Python List Average Median What’s the median of a Python list? Formally, the median is “the value separating the higher half from the lower half of a data sample” (wiki). ![]() How to calculate the median of a Python list?
Together, you can simply get the median by executing the expression Code: median = sorted(income)[len(income)//2]Here’s the concrete code example: Code: income = [80000, 90000, 100000, 88000] average = sum(income) / len(income)
median = sorted(income)[len(income)//2] print(average)
# 89500.0 print(median)
# 90000.0Related tutorials: Python List Average Mean The mean value is exactly the same as the average value: sum up all values in your sequence and divide by the length of the sequence. You can use either the calculation Code: sum(list) / len(list)Code: statisticsCode: mean(list)Here are both examples: Code: lst = [1, 4, 2, 3] # method 1
average = sum(lst) / len(lst)
print(average)
# 2.5 # method 2
import statistics
print(statistics.mean(lst))
# 2.5Both methods are equivalent. The Code: statisticsCode: mean()mean() | Arithmetic mean (“average”) of data. median() | Median (middle value) of data. median_low() | Low median of data. median_high() | High median of data. median_grouped() | Median, or 50th percentile, of grouped data. mode() | Mode (most common value) of discrete data. These are especially interesting if you have two median values and you want to decide which one to take. Python List Average Standard Deviation Standard deviation is defined as the deviation of the data values from the average (wiki). It’s used to measure the dispersion of a data set. You can calculate the standard deviation of the values in the list by using the statistics module: Code: import statistics as s lst = [1, 0, 4, 3]
print(s.stdev(lst))
# 1.8257418583505538In contrast to the average, there are Python built-in functions that calculate the minimumand maximumof a given list. The Code: min(list)Code: max(list)Here’s an example of the minimum, maximum and average computations on a Python list: Code: import statistics as s lst = [1, 1, 2, 0]
average = sum(lst) / len(lst)
minimum = min(lst)
maximum = max(lst) print(average)
# 1.0 print(minimum)
# 0 print(maximum)
# 2How to calculate the average using the Code: sum()Code: sum(list)Again, the following example shows how to do this: Code: import statistics as s lst = [1, 1, 2, 0]
average = sum(lst) / len(lst) print(average)
# 1.0Python’s package for data science computation NumPy also has great statistics functionality. You can calculate all basic statistics functions such as average, median, variance, and standard deviation on NumPy arrays. Simply import the NumPy library and use the Code: np.average(a)Code: aHere’s the code: Code: import numpy as np a = np.array([1, 2, 3])
print(np.average(a))
# 2.0NumPy’s average function computes the average of all numerical values in a NumPy array. When used without parameters, it simply calculates the numerical average of all values in the array, no matter the array’s dimensionality. For example, the expression Code: np.average([[1,2],[2,3]])Code: (1+2+2+3)/4 = 2.0https://repl.it/repls/SwelteringTornApplets?lite=true However, what if you want to calculate the weighted average of a NumPy array? In other words, you want to overweight some array values and underweight others. You can easily accomplish this with NumPy’s average function by passing the weights argument to the NumPy Code: averageCode: import numpy as np a = [-1, 1, 2, 2] print(np.average(a))
# 1.0 print(np.average(a, weights = [1, 1, 1, 5]))
# 1.5In the first example, we simply averaged over all array values: Code: (-1+1+2+2)/4 = 1.0Code: (-1+1+2+(2+2+2+2+2))/8 = 1.5Let’s explore the different parameters we can pass to Code: np.average(...)
Here is an example how to average along the columns of a 2D NumPy array with specified weights for both rows. Code: import numpy as np # daily stock prices
# [morning, midday, evening]
solar_x = np.array(
[[2, 3, 4], # today
[2, 2, 5]]) # yesterday # midday - weighted average
print(np.average(solar_x, axis=0, weights=[3/4, 1/4])[1])What is the output of this puzzle? *Beginner Level* (solution below) You can also solve this puzzle in our puzzle-based learning app (100% FREE): Test your skills now! Related article: Python Average List of Dictionaries Problem: Given is a list of dictionaries. Your goal is to calculate the average of the values associated to a specific key from all dictionaries. Example: Consider the following example where you want to get the average value of a list of database entries (e.g., each stored as a dictionary) stored under the key Code: 'age'Code: db = [{'username': 'Alice', 'joined': 2020, 'age': 23}, {'username': 'Bob', 'joined': 2018, 'age': 19}, {'username': 'Alice', 'joined': 2020, 'age': 31}] average = # ... Averaging Magic Here ... print(average)The output should look like this where the average is determined using the ages Code: (23+19+31)/3 = 24.333Solution: Solution: You use the feature of generator expression in Python to dynamically create a list of Code: ageCode: ageCode: ageCode: db = [{'username': 'Alice', 'joined': 2020, 'age': 23}, {'username': 'Bob', 'joined': 2018, 'age': 19}, {'username': 'Alice', 'joined': 2020, 'age': 31}] average = sum(d['age'] for d in db) / len(db) print(average)
# 24.333333333333332Let’s move on to the next question: how to calculate the average of a list of floats? Python Average List of Floats Averaging a list of floats is as simple as averaging a list of integers. Just sum them up and divide them by the number of float values. Here’s the code: Code: lst = [1.0, 2.5, 3.0, 1.5]
average = sum(lst) / len(lst)
print(average)
# 2.0Problem: How to average all values if the values are stored in a list of tuples? Example: You have the list of tuples Code: [(1, 2), (2, 2), (1, 1)]Code: (1+2+2+2+1+1)/6 = 1.5Solution: There are three solution ideas:
Next, I’ll give all three examples in a single code snippet: Code: lst = [(1, 2), (2, 2), (1, 1)] # 1. Unpacking
lst_2 = [*lst[0], *lst[1], *lst[2]]
print(sum(lst_2) / len(lst_2))
# 1.5 # 2. List comprehension
lst_3 = [x for t in lst for x in t]
print(sum(lst_3) / len(lst_3))
# 1.5 # 3. Nested for loop
lst_4 = []
for t in lst: for x in t: lst_4.append(x)
print(sum(lst_4) / len(lst_4))
# 1.5Unpacking: The asterisk operator in front of an iterable “unpacks” all values in the iterable into the outer context. You can use it only in a container data structure that’s able to catch the unpacked values. List comprehension is a compact way of creating lists. The simple formula is [ expression + context ].
The example Code: [x for x in range(3)]Code: [0, 1, 2]Python Average Nested List Problem: How to calculate the average of a nested list? Example: Given a nested list [[1, 2, 3], [4, 5, 6]]. You want to calculate the average (1+2+3+4+5+6)/6=3.5. How do you do that? Solution: Again, there are three solution ideas:
Next, I’ll give all three examples in a single code snippet: Code: lst = [[1, 2, 3], [4, 5, 6]] # 1. Unpacking
lst_2 = [*lst[0], *lst[1]]
print(sum(lst_2) / len(lst_2))
# 3.5 # 2. List comprehension
lst_3 = [x for t in lst for x in t]
print(sum(lst_3) / len(lst_3))
# 3.5 # 3. Nested for loop
lst_4 = []
for t in lst: for x in t: lst_4.append(x)
print(sum(lst_4) / len(lst_4))
# 3.5Unpacking: The asterisk operator in front of an iterable “unpacks” all values in the iterable into the outer context. You can use it only in a container data structure that’s able to catch the unpacked values. List comprehension is a compact way of creating lists. The simple formula is [ expression + context ].
The example Code: [x for x in range(3)]Code: [0, 1, 2]Where to Go From Here [i]Python 3.x doesn’t have a built-in method to calculate the average. Instead, simply divide the sum of list values through the number of list elements using the two built-in functions Code: sum()Code: len()Code: listCode: sum(list)/len(list)If you keep struggling with those basic Python commands and you feel stuck in your learning progress, I’ve got something for you: Python One-Liners (Amazon Link). In the book, I’ll give you a thorough overview of critical computer science topics such as machine learning, regular expression, data science, NumPy, and Python basics—all in a single line of Python code! Get the book from Amazon! OFFICIAL BOOK DESCRIPTION: Python One-Liners will show readers how to perform useful tasks with one line of Python code. Following a brief Python refresher, the book covers essential advanced topics like slicing, list comprehension, broadcasting, lambda functions, algorithms, regular expressions, neural networks, logistic regression and more. Each of the 50 book sections introduces a problem to solve, walks the reader through the skills necessary to solve that problem, then provides a concise one-liner Python solution with a detailed explanation. |