Just doing an exercise question as follows: Take a ' number square' as a parameter and returns a list of the column sums.
e.g
square = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
]
Result should be =
[28, 32, 36, 40]
What I have so far:
def column_sums(square):
col_list = []
for i in range(len(square)):
col_sum = 0
for j in range(len(square[i])):
col_sum += square[j]
col_list.append(col_sum)
return col_list
So I think I have all the parameters laid out, with all the indexing correct, but I am stuck because I get an error
builtins.TypeError: unsupported operand type(s) for +=: 'int' and 'list'
Which shouldn't happen if I am referencing the element within the list I thought.
Also it's probably easier to use the SUM command here, but couldn't figure out a way to use it cleanly.
Adding elements from separate lists via indexing.