Aug-13-2021, 06:19 AM
You could transpose the rows into columns and then use sum
from itertools import zip_longest rows = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] print(rows) def transpose_rows(rows): return tuple(zip_longest(*rows, fillvalue=0)) columns = transpose_rows(rows) print(columns) print(sum(columns[3]))
Output:[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
((1, 5, 9, 13), (2, 6, 10, 14), (3, 7, 11, 15), (4, 8, 12, 16))
40