I was trying to make a definition that would add all numbers within each sublist in a list of lists.
def MassAddition(_list):
output = []
total = 0
for i in _list:
if isinstance(i, list):
output.append(MassAddition(i))
else:
total = total + i
output.append(total)
return output
Problem is that it returns an extra item in a list at the end. I think its because I made total = 0 and then appended it to output list outside of for loop. Can someone help me clean this up? Ps. This definition should be able to handle any level of nested lists.
example:
input = [[0,1,2], [2,1,5],[2,2,2],2,2,1]
desiredoutput = [[3],[8],[6],5]
Thank you,