Replace if true, uneven list

I have a list of lists that contain names, based on those names I want to replace them with different text. I have multiple search terms. That seems to be working, however I can’t get the single item in teh list 2 to replace all true items in list 1. The null items in my out list should be THRD.

@Evan.Pond Would a python solution work?

OUT = [[IN[2] if item in IN[1] else item for item in list] for list in IN[0]]

@Evan.Pond If not, try this:

I’ve used == node but you will get the same result with the String.Contains node as well.

1 Like

Python works, thanks! I have no idea how to write python, it’s next on my list to learn. Fr now I will setting for copy/paste!
Any way to make it not case sensitive? If not I will just add to my list.

1 Like

@Evan.Pond Yes, sir. It is possible to ignore the case.
Try this:

toNestList = lambda x: x if any(isinstance(i, list) for i in x) else [x]
OUT = [[IN[2] if item.lower() in map(str.lower,IN[1]) else item for item in list] for list in toNestList(IN[0])]
1 Like

Thanks for the help, this gets me what I need.

1 Like

One more ask, I forgot to mention the the input list is more robust then I showed.
My search needs to be “contains”:

@Evan.Pond It is still doable.


The one on top is case-sensitive and the bottom one is case-insensitive.
Using the case-insensitive one may also lead to some unexpected replacements. e.g. It replaced Br in Brass
See which script works best for you.

Case-sensitive script:

def replace_words(strText):
    for searchWord in IN[1]:
        strText = strText.replace(searchWord, IN[2])
    return strText

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

OUT = [[replace_words(item) for item in list] for list in toNestList(IN[0])]

Case-insensitive script:

import re

def replace_words(strText):
    for searchWord in IN[1]:
    	pattern = re.compile(searchWord, re.IGNORECASE)
        strText = pattern.sub(IN[2],strText)
    return strText

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

OUT = [[replace_words(item) for item in list] for list in toNestList(IN[0])]
1 Like

That you so much for all your help, I should have been more clear.
When a line item has anything in the search list I want the whole thing to be replaced.
I am inputting a list of fittings and I want to output the list as to the pipe end prep needed:

Capture

@Evan.Pond Here you go

toNestList = lambda x: x if any(isinstance(i, list) for i in x) else [x]
OUT = [[IN[2] if any(st.lower() in item.lower() for st in IN[1]) else item for item in list] for list in toNestList(IN[0])]