Reverse index in a list
I think you're overthinking this:
First, reverse the list:
inverselist = k1[::-1]
Then, replace the first nonzero element:
for i, item in enumerate(inverselist):
if item:
inverselist[i] += 100
break
If you want to reverse, you can just do it by slicing. As below,
>>> a = [1,2,3]
>>> reverse_a = a[::-1]
>>> reverse_a
[3, 2, 1]
Once you go through the list, you just need to check when the first element is a non-zero element
k1=[31.0, 72, 105.0, 581.5, 0, 0, 0]
newk1= k1[::-1]
for i in range(len(newk1)):
if newk1[i] != 0:
newk1[i] += 100
break
print("Newk1", newk1 ) #prints Newk1 [0, 0, 0, 681.5, 205.0, 172, 131.0]
Just a silly way. Modifies the list instead of creating a new one.
k1.reverse()
k1[list(map(bool, k1)).index(1)] += 100