Fundamentals 5 min read

Three Elegant Python Tricks to Sum Columns in a 2D List

This article explains a Python fan's question about summing each column of a two‑dimensional list and presents three concise solutions—including list unpacking with zip, NumPy aggregation, and a functional reduce approach—complete with code snippets and explanations.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Three Elegant Python Tricks to Sum Columns in a 2D List

The author, a Python enthusiast, shares a question from a fan about summing columns of a 2D list.

1. Problem Statement

A list of lists is given, and the goal is to compute the sum of each column without manually defining a variable for each element.

lst = [[1, 2, 3, 4], [1, 5, 1, 2], [2, 3, 4, 5], [5, 3, 1, 3]
s1 = 0
s2 = 0
s3 = 0
s4 = 0
for i in lst:
    s1 += i[0]
    s2 += i[1]
    s3 += i[2]
    s4 += i[3]
print(list([s1, s2, s3, s4]))

The above works but is not scalable for many columns.

2. Solutions

Solution by Yuliang

lst = [[1, 2, 3, 4], [1, 5, 1, 2], [2, 3, 4, 5], [5, 3, 1, 3]]
[print(sum(i)) for i in zip(*lst)]

This uses list unpacking with zip to sum each column.

Solution by Daler (using NumPy)

import numpy as np

lst = [[1, 2, 3, 4], [1, 5, 1, 2], [2, 3, 4, 5], [5, 3, 1, 3]]

list1 = np.sum(lst, axis=0)  # column-wise sum
list2 = np.sum(lst, axis=1)  # row-wise sum
print(list1)
print(list2)

NumPy provides concise functions for both column and row sums.

Solution by Moon God

from functools import reduce

lst = [[1, 2, 3, 4],
       [1, 5, 1, 2, 6],
       [2, 3, 4, 5],
       [5, 3, 1, 3]]

print(list(reduce(lambda x, y: map(lambda i, j: i + j, x, y), lst)))

This approach uses reduce and map with a lambda to aggregate column sums.

3. Conclusion

The article presented three different methods to sum corresponding elements of sub‑lists in a 2D list, demonstrating plain Python loops, list unpacking with zip, NumPy operations, and functional programming with reduce. Readers are encouraged to try alternative solutions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonreducelist-comprehensionlist sum
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.