Comming from this question Adding the last position of an array to the same array
I was curious if the mentioned loop can be done in a list comprehension?
array = [3,4,2,5,4,5,8,7,8,9]
value = 10
for i in range(1,10):
array[i] = array[i-1] + value
I thought maybe with the walrus operator.
My attempt gave me an error which lead to cannot-use-assignment-expressions-with-subscript
[array[count] := val if count == 0 else array[count] := array[count-1] + value for count,val in enumerate(array)]
Any ideas?
The for-loop only uses the first element and overwrites array
with brand new items. The very same outcome can be achieved using the below list comprehension:
array = [array[0] + i*value for i in range(len(array))]
Output:
[3, 13, 23, 33, 43, 53, 63, 73, 83, 93]