# How to replace multiple strings in a list with sublists

**URL:** <https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523>\
**Category:** Revit\
**Tags:** revit, python, dynamo\
**Created:** [March 18, 2022, 11:46am UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523 "2022-03-18T11:46:46Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [March 18, 2022, 11:46am UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/1 "2022-03-18T11:46:46Z")

</div>

How to replace multiple strings in a list with sublists?

I am trying to replace a list composed of 8 strings if they are contaned into a list of many sublists of strings. I found useful the clockwork package node Replace.Multiple but I would like to do it with designscript or python.

it works sometimes…but the custom node takes quite a lot time to replace strings, then looking for alternative

this example does not work, I thought it does not work with Empt Lists and Nulls but it works within a custom node package…so **I guess it does not work with a list of sublists strings as input**  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/c/9/c92c2506426f03f31aee75a78d3b829b58ced5eb.png)  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/b/f/bf4f4f167c3608fd560512dbff909c56d12d7f16.png)  
the code:

```auto
import clr
import re

def ReplaceMultiple(str, searchstr, replacestr, sort):
	replacements = dict(zip(searchstr, replacestr))
	if sort: substrs = sorted(replacements, key=len, reverse=True)
	else: substrs = searchstr
	regexp = re.compile('|'.join(map(re.escape, substrs)))
	return regexp.sub(lambda match: replacements[match.group(0)], str)
	
if isinstance(IN[1], list): searchstr = IN[1]
else: searchstr = [IN[1]]
if isinstance(IN[2], list): replacestr = IN[2]
else: replacestr = [IN[2]]

if isinstance(IN[0], list): OUT = [ReplaceMultiple(x, searchstr, replacestr, False) for x in IN[0]]
else: OUT = ReplaceMultiple(IN[0], searchstr, replacestr, False)

```

---

<div class="post-metadata">

**Author:** ![haganjake2](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/haganjake2/32/119069_2.png) [@haganjake2](https://forum.dynamobim.com/u/haganjake2)\
**Post date:** [March 18, 2022, 11:59am UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/2 "2022-03-18T11:59:49Z")

</div>

Can you share an image of the structure of the string list you wish to replace within?

Hard to know how to script it without seeing the structure you’re looking at.

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [March 18, 2022, 2:39pm UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/3 "2022-03-18T14:39:24Z")

</div>

There have been a couple similar topics posted in just the last few days. Have you checked them out yet?

---

<div class="post-metadata">

**Author:** ![Anton\_Huizinga](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/anton_huizinga/32/155377_2.png) [@Anton\_Huizinga](https://forum.dynamobim.com/u/Anton_Huizinga)\
**Post date:** [March 18, 2022, 2:50pm UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/4 "2022-03-18T14:50:38Z")

</div>

> [@ruben.romero](#):
>
> clockwork

Aren’t all ClockWork nodes just Python scripts in a Custom Node?

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [March 18, 2022, 6:45pm UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/6 "2022-03-18T18:45:25Z")

</div>

> [@ruben.romero](#):
>
> **I guess it does not work with a list of sublists strings as input**

That seems to be correct based on the error. Have you tried using list levels with the node?

---

<div class="post-metadata">

**Author:** ![Anton\_Huizinga](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/anton_huizinga/32/155377_2.png) [@Anton\_Huizinga](https://forum.dynamobim.com/u/Anton_Huizinga)\
**Post date:** [March 19, 2022, 9:22am UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/7 "2022-03-19T09:22:43Z")

</div>

You mean something like this?

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/0/7048016397ce3902b95389d56cb661cab8b2c274.png)

```auto
// Search the texts
texts: string[][];

// Search pattern that must be replaced
searchtexts: string[];

// Replace pattern
replacetexts: string[];

// Function to replace multiple texts
[Imperative]
{
  result = [];

  // Loop through all texts
  for (i in 0..List.Count(texts)-1)
  {
    currenttext = texts[i];

    // Loop through the search patterns that must be replaced
    for (j in 0..List.Count(searchtexts)-1)
      {
        currenttext = String.Replace(currenttext, searchtexts[j], replacetexts[j]);
      }

      // Add it to the result
      result[i] = currenttext;
  }

  return result;
};

```

---

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [March 21, 2022, 9:15am UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/8 "2022-03-21T09:15:47Z")

</div>

> [@Anton\_Huizinga](#):
>
> something like this?

some error, it works with `DSCore.String`.

**why is not that possible to achieve with the OOTB node String.Replace? the code seems to do same**

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/0/7070c195630fbaa522fe1f93891ce0fcb12e4a11.png)

---

<div class="post-metadata">

**Author:** ![Anton\_Huizinga](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/anton_huizinga/32/155377_2.png) [@Anton\_Huizinga](https://forum.dynamobim.com/u/Anton_Huizinga)\
**Post date:** [March 21, 2022, 10:07am UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/9 "2022-03-21T10:07:26Z")

</div>

You probably have other packages that contain a String object. In that case you must point to DSCore.String to use the ootb functions.

---

<div class="post-metadata">

**Author:** ![PauLtus](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/paultus/32/48502_2.png) [@PauLtus](https://forum.dynamobim.com/u/PauLtus)\
**Post date:** [March 21, 2022, 10:34am UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/10 "2022-03-21T10:34:52Z")

</div>

I agree.  
@ruben.romero, what you’re asking is probably perfectly possible but we can’t really figure out what you’re trying to achieve like this. Can you just show an example of what your input list looks like and what you want your output list to look like?

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [March 21, 2022, 11:41am UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/12 "2022-03-21T11:41:38Z")

</div>

Hi,  
Here is a variation of the python code (1st post)

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/b/c/bc5897cbe065976128bb96395c11b726e32e21b5.png)

```auto
import sys
import clr
import re
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

def replacetext(text):
    global dict_replace
    if text is not None:
        rep = dict((re.escape(k), v) for k, v in dict_replace.items()) 
        pattern = re.compile("|".join(rep.keys()))
        text = pattern.sub(lambda m: rep[re.escape(m.group(0))], text)
    return text

def nestedRecursFunc(lst, functoApply):
    newlst = list(lst)[:] # copy input list and convert it (if necessary)
    for idx, elem in enumerate(lst):
        if isinstance(elem, list) and len(elem) > 0:
            newlst[idx] = nestedRecursFunc(lst[idx], functoApply)
        elif isinstance(elem, list) and len(elem) == 0:
            newlst[idx] = []
        else:
            newlst[idx] = functoApply(lst[idx])

    return newlst
    
multi_NestedList = IN[0]
dict_replace = dict(IN[1])

OUT = nestedRecursFunc(multi_NestedList, replacetext)

```

---

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [March 21, 2022, 1:55pm UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/13 "2022-03-21T13:55:26Z")

</div>

> [@c.poupin](#):
>
> Here is a variation of the python code (1st post)

hello, the python code did not work because this error warning. the designscript worked well.  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/e/e/ee7145f494c14180803278f76187eacbcc1a19f1.png)

---

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [March 21, 2022, 2:05pm UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/14 "2022-03-21T14:05:54Z")

</div>

> [@haganjake2](#):
>
> Can you share an image of the structure of the string list you wish to replace within?
> 
> Hard to know how to script it without seeing the structure you’re looking at.

@PauLtus something like this, @Anton_Huizinga I tried python and designscript, and the python of custom node was double faster with many items in the string list input

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/1/011faf978d2311757b27aab70a1b6e29da99ef3c.png)

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [March 21, 2022, 2:19pm UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/15 "2022-03-21T14:19:41Z")

</div>

This should work.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/4/14df6fe17d02dddf1794c808c9760dd4bf57d1c8.png)

> **Python Code**
>
> ```auto
> subs = IN[0]
> searchStr = IN[1]
> replaceStr = IN[2]
> out = []
> 
> for sub in subs:
> output = []
> for s in sub:
> for search,replace in zip(searchStr,replaceStr):
> s = s.replace(search,replace)
> output.append(s)
> out.append(output)
> 
> OUT = out
> 
> ```

---

<div class="post-metadata">

**Author:** ![PauLtus](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/paultus/32/48502_2.png) [@PauLtus](https://forum.dynamobim.com/u/PauLtus)\
**Post date:** [March 21, 2022, 2:24pm UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/16 "2022-03-21T14:24:53Z")

</div>

Your list structure is kinda weird, if it’s always like this, it’s okay but if you want that to work consistently it might actually be the easiest to string together a bunch of `String.Replace` nodes.

Anyway, a bit of Python is still more elegant so here we go:

```auto
txtlist = IN[0]
oldlist = IN[1]
replist = IN[2]

outlist=[]
for txt1 in txtlist:
	out = []
	for txt in txt1:
		for old,rep in zip(oldlist,replist):
			txt.replace(old,rep)
		out.append(txt)
	outlist.append(out)

OUT = outlist

```

Edit: @Nick_Boyts had the exact same idea, it seems.

---

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [March 21, 2022, 2:34pm UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/17 "2022-03-21T14:34:26Z")

</div>

> [@Nick\_Boyts](#):
>
> Python Code

many thanks @Nick_Boyts @Anton_Huizinga @PauLtus@c.poupin , I also tried that code which I found in other forum posts before write my post initially and I remember to have a warning message in the python node. Now I tried and it works fine, I did a comparison between the solution with Designscript as well and you can check run time results, Python wins.

thanks great answers to everyone, but disappointed with the python I wrote even looking smart and the @c.poupin version as well.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/5/7/57e8bd5c9a7659a757bbde46aec6ffde9fe68414.png)

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [March 21, 2022, 2:45pm UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/18 "2022-03-21T14:45:56Z")

</div>

> [@ruben.romero](#):
>
> Python wins

Python is almost always going to win in a head-to-head comparison. Especially with looping.

---

<div class="post-metadata">

**Author:** ![christian.stan](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/christian.stan/32/166296_2.png) [@christian.stan](https://forum.dynamobim.com/u/christian.stan)\
**Post date:** [May 28, 2022, 6:42am UTC](https://forum.dynamobim.com/t/how-to-replace-multiple-strings-in-a-list-with-sublists/74523/20 "2022-05-28T06:42:55Z")

</div>

hello, in advance sorry for the digression  
Depending on what you “claim”, it is more efficient to switch to Python as soon as you have loops rather than stay in Designscript,  
where it was humorous (note for jokes.)

Cordially  
christian.stan
