Reverse index in a list
Question:
For the list k1=[31.0, 72, 105.0, 581.5, 0, 0, 0], I would like to add a constant for example 100 to the first non-zero element in the reverse list.
this is what I want: newk1=[0, 0, 0, 681.5, 105, 72, 31]
As a beginner in Python I could not figure it out. Could you please help me. That is my code:
k1=[31.0, 72, 105.0, 581.5, 0, 0, 0]
Inverselist=[]
for i in range(len(etack1)):
Inverselist.append(etack1[-(i+1)])
print("Inverselist", Inverselist)
newk1=Inverselist
run_once = 0
while run_once < 1:
for j in range(len(newk1)):
if newk1[j-1]>0:
newk1[j-1]=newk1[j-1]+100
run_once = 1
break
print("Newk1", newk1 )
Answers:
You can try this:
k1=[31.0, 72, 105.0, 581.5, 0, 0, 0]
new_list = []
flag = False
for i in k1[::-1]:
if i > 0 and not flag:
new_list.append(i+100)
flag = True
else:
new_list.append(i)
Output:
[0, 0, 0, 681.5, 105.0, 72, 31.0]
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
Here’s a solution using reversed
instead of slicing with [::-1]
:
items = [31.0, 72, 105.0, 581.5, 0, 0, 0]
inverse_items = []
found_non_zero = False
for item in reversed(items):
if not found_non_zero and item:
found_non_zero = True
item += 100
inverse_items.append(item)
list1 = [31.0, 72, 105.0, 581.5, 0, 0, 0]
last_idx = len(list1)-1
print(last_idx)
for i in range(len(list1)):
list1.insert(i,list1.pop(last_idx))
print(list1)
Output :
[0, 0, 0, 581.5, 105.0, 72, 31.0]
For the list k1=[31.0, 72, 105.0, 581.5, 0, 0, 0], I would like to add a constant for example 100 to the first non-zero element in the reverse list.
this is what I want: newk1=[0, 0, 0, 681.5, 105, 72, 31]
As a beginner in Python I could not figure it out. Could you please help me. That is my code:
k1=[31.0, 72, 105.0, 581.5, 0, 0, 0]
Inverselist=[]
for i in range(len(etack1)):
Inverselist.append(etack1[-(i+1)])
print("Inverselist", Inverselist)
newk1=Inverselist
run_once = 0
while run_once < 1:
for j in range(len(newk1)):
if newk1[j-1]>0:
newk1[j-1]=newk1[j-1]+100
run_once = 1
break
print("Newk1", newk1 )
You can try this:
k1=[31.0, 72, 105.0, 581.5, 0, 0, 0]
new_list = []
flag = False
for i in k1[::-1]:
if i > 0 and not flag:
new_list.append(i+100)
flag = True
else:
new_list.append(i)
Output:
[0, 0, 0, 681.5, 105.0, 72, 31.0]
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
Here’s a solution using reversed
instead of slicing with [::-1]
:
items = [31.0, 72, 105.0, 581.5, 0, 0, 0]
inverse_items = []
found_non_zero = False
for item in reversed(items):
if not found_non_zero and item:
found_non_zero = True
item += 100
inverse_items.append(item)
list1 = [31.0, 72, 105.0, 581.5, 0, 0, 0]
last_idx = len(list1)-1
print(last_idx)
for i in range(len(list1)):
list1.insert(i,list1.pop(last_idx))
print(list1)
Output :
[0, 0, 0, 581.5, 105.0, 72, 31.0]