I need to write a recursive function to return sums of all possible subsets. I wrote the following code:
def subsets_sums(lst):
if len(lst) == 0:
return 0
else:
sum_list = [sum(lst)]
for i in range(len(lst)):
index_list = lst.copy()
del index_list[i]
test_list = subsets_sums(index_list)
sum_list = test_list, sum_list
return sum_list
But the output is very inelegant due to all the lists concatenations. something like:
(((0, [10]), ((0, [3]), [13])), (((0, [10]), ((0, [6]), [16])), (((0, [3]), ((0, [6]), [9])), [19])))
for the list:
[10,3,6]
How can I make it appear in a list like:
[0,10,0,3,13,0,10,0,6,16,0,3,0,6,9,19]
Can I change something within the code?