title: Efficient way of multiple replace in large string
body: <p>I want to join multiple sets of characters iteratively in a string. Example:</p>
  <pre class="lang-py prettyprint-override"><code>mystr = 'T h i s _ i s _ a _ s e n t e n c e'
  joins = [('e', 'n'), ('en', 't'), ('i', 's'), ('h', 'is')]
  
  # do multiple replace
  for bigram in joins:
    mystr = mystr.replace(' '.join(bigram), ''.join(bigram))
  print(mystr)
  'T his _ is _ a _ s ent en c e'
  </code></pre>
  <p>In the first iteration it joins <code>e n</code> into <code>en</code>, then <code>en t</code> into <code>ent</code> and so on. It's important that the joins are done in order, since the join ('en', 't') doens't work unless ('e', 'n') has been joined.</p>
  <p>With a string of 20MB and 10k joins, this takes a while. I'm looking to optimize this, but I don't know how. Some of the things I've discarded:</p>
  <ul>
  <li>I didn't use regex like in this <a href="https://stackoverflow.com/questions/42742810/speed-up-millions-of-regex-replacements-in-python-3/42747503#42747503">question</a> because I don't know how to do <code>re.sub</code> where the substitution is the match itself but joined together</li>
  <li>I didn't use <code>str.translate</code> like this <a href="https://stackoverflow.com/a/44742930/4569908">question</a> either because as far as I know, translate can only translate single characters, and in my <code>joins</code> there are multiple</li>
  </ul>
  <p>Is there any algorithm, string or regex or any other function that would allow me to do this? Thank you!</p>