I have a list [0, 1, 2, 3, 4, 5, 6]
and I sum its parts so that:
l = [0, 1, 2, 3, 4, 5, 6] -> 21
l = [1, 2, 3, 4, 5, 6] -> 21
l = [2, 3, 4, 5, 6] -> 20
l = [3, 4, 5, 6] -> 18
l = [4, 5, 6] -> 15
l = [5, 6] -> 11
l = [6] -> 6
l = [] -> 0
So, I get the corresponding sums of the list's parts: [21, 21, 20, 18, 15, 11, 6, 0]
The code I use is:
[sum(l[i:]) for i in range(len(l) + 1)]
But, for lists with range greater than 100000
the code slows down significantly.
Any idea why and how to optimize it?