A shorter representation with for loop inside a list

Hi,
I shorten the presentation with a for loop inside a list

a = [1,2,3,4,5]
[a[:k][-1]/sum(a[:k]) for k in range(1, len(a)+1)]

Results

[1.0, 0.6666666666666666, 0.5, 0.4, 0.3333333333333333]

It seems too concise to read. Is there any other way to write such as

[x[-1]/sum(x) for x := a[:k] for k in range(1, len(a)+1)]

in order to represent x = a[:k] ? The last statement yields an error message though

If you want shorter try:

[a[k-1]/sum(a[:k]) for k in range(1, len(a)+1)]

Even shorter is:

[a[k]/sum(a[:k+1]) for k in range(len(a))]
1 Like