Problem: You have a list of lists and you want to calculate the average of the different columns.
Example: Given the following list of lists with four rows and three columns.
data = [[0, 1, 0], [1, 1, 1], [0, 0, 0], [1, 1, 0]]
You want to have the average values of the three columns:
[average_col_1, average_col_2, average_col_3]
There are three methods that solve this problem. You can play with them in the interactive shell and read more details below:
Method 1: Average in Python (No Library)
data = [[0, 1, 0], [1, 1, 1], [0, 0, 0], [1, 1, 0]] # Method 1: Pure Python res = [sum(x) / len(x) for x in zip(*data)] print(res) # [0.5, 0.75, 0.25]
Do you love Python one-liners? I do for sure—I’ve even written a whole book about it with San Francisco Publisher NoStarch. Click to check out the book in a new tab:
You can visualize the code execution and memory objects of this code in the following tool (just click “Next” to see how one step of the code unfolds).
Method 2: Average with NumPy Library
data = [[0, 1, 0], [1, 1, 1], [0, 0, 0], [1, 1, 0]] # Method 2: NumPy import numpy as np a = np.array(data) res = np.average(a, axis=0) print(res) # [0.5 0.75 0.25]
axis argument of the average function defines along which axis you want to calculate the average value. If you want to average columns, define
axis=0. If you want to average rows, define
axis=1. If you want to average over all values, skip this argument.
Method 3: Mean Statistics Library + Map()
Just to show you another alternative, here’s one using the
map() function and our
zip(*data) trick to transpose the “matrix”
data = [[0, 1, 0], [1, 1, 1], [0, 0, 0], [1, 1, 0]] # Method 3: Statistics + Map() import statistics res = map(statistics.mean, zip(*data)) print(list(res)) # [0.5, 0.75, 0.25]
map(function, iterable) function applies
function to each element in
iterable. As an alternative, you can also use list comprehension as shown in method 1 in this tutorial. In fact, Guido van Rossum, the creator of Python and Python’s benevolent dictator for life (BDFL), prefers list comprehension over the
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.