data = [[1, 1, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 1, 0], [0, 0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 2, 3], [1, 1, 2, 0, 1, 1, 0], [1, 0, 0, 2, 1, 1, 2]]
Hello, I have this list with nested lists called data. I would like to sum up the indivual nested lists only so the output is:
[[4],[2],[2],[0],[0],[0],[0],[0],[6],[6],[7]]
I tried to do this:
for numbers in data:
new_list = []
y = sum(numbers)
new_list.append(y)
print (new_list)
However, this only gives the sum of the last nested list, because for every for-loop the new_list seems to "reset".
Thanks
new_list = [], then appending to it, then on the next iteration, doingnew_list = [], which discards the previous list.list(map(sum, data))? You coudl get what you posted originally, though, with something like[[sum(numbers)] for numbers in data]which you coudl get in your for-loop by just doingnew_list.append([y])