Replace index value from the value of index directly before it


Hello everyone!!

I am trying to replace the “sab” string to the value that is directly above it. basically, I want the outcome to be as such:

10
10
10
20
30
30

any feedback will be appreciated. thank you!

Kathir

Hi @C3312854,

Try this:

for i,x in enumerate(IN[0]):
   if IN[0][i] == IN[1]:
     IN[0][i] = IN[0][i-1]
     
OUT = IN[0]
1 Like

Hello Shah! Thank you for the help, I will try this and let you know. By the way does this code also work for sub lists? If not please do let me know on how i can modify the code. I have no knowledge on python, and your feedback will be appreciated.

kathir

@C3312854,

This should do the trick for sublists.

toNestList = lambda x: x if any(isinstance(i, list) for i in x) else [x]

for sub in toNestList(IN[0]):
    for i,x in enumerate(sub):
       if sub[i] == IN[1]:
         sub[i] = sub[i-1]
     
OUT = IN[0]
2 Likes

hello shah!

I will try this out and let you know thank you!!

kathir

Hi Shah!

Thank you for helping me with this!!

with regards

Kathir

1 Like