I have been asked to find the sum of the first value in each column in a nested list. How do I do this without using imports or the sum function?
def column_sums(square):
"""Returns the sum of the columns"""
result = []
i = 0
for element in square:
n = 0
for item in element:
if item == element[n]:
i = i + item
n = n + 1
result.append(i)
i = 0
return result
square = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
]
print(column_sums(square))
This is what I have come up with but it only returns the value of the first column. How can I get the sum of all the columns set out like this?:
[28, 32, 36, 40]