Reverse index in a list

28,903

Solution 1

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

Solution 2

Just a silly way. Modifies the list instead of creating a new one.

k1.reverse()
k1[list(map(bool, k1)).index(1)] += 100

Solution 3

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]

Solution 4

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]
Share:
28,903
rezzz
Author by

rezzz

Updated on July 09, 2022

Comments

  • rezzz
    rezzz almost 2 years

    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 )
    
  • Admin
    Admin over 6 years
    This also creates two copies of the original list. Not that it matters much for such a small data set, but you could do for i in reversed(k1) instead.
  • Ajax1234
    Ajax1234 over 6 years
    @StefanPochmann I added a more generic solution. Please see my recent edit.
  • Code-Apprentice
    Code-Apprentice over 6 years
    @rezi Note how this answer first describes the steps in words. Then after having a clear idea of each step, the words are translated into Python. This first step is often skipped by beginning programmers and is the most important. I cannot emphasize this enough. The most important step in programming is to understand the problem and describe a solution in words.
  • Admin
    Admin over 4 years
    This code just reverses the existing list without using an additional list.One thing i missed is to add 100 to the first elemet.we can use the code suggested above to do that,
  • Admin
    Admin over 4 years
    u pop the last element in the list and insert in the order of the list starting from index 0.This will reverse the list automatically.
  • Brambor
    Brambor about 4 years
    .index(True) sounds better, also an explanation of your answer is missing on a beginner's question.
  • Brambor
    Brambor about 4 years
    And then, reverse it back?
  • Tim Pietzcker
    Tim Pietzcker about 4 years
    @Brambor: I don't think so, OP wanted a reversed list as the result
  • Brambor
    Brambor about 4 years
    @TimPietzcker You are right, I read the language, which I interpreted differently and not the explicit "this is what I want" :D