stack.reverse()

def replace(stack, parts):
    if not stack:
        return parts
stack = list(stack)
from_, to = stack.pop()
split_parts = [replace(stack, part.split(from_)) for part in parts]
parts = [to.join(split_subparts) for split_subparts in split_parts]
return parts
return replace(stack, [text])[0]
print(multi_replace([('foo', 'bar'), ('baaz', 'foo'), ('quux', 'moop')],
    'foobarbaazfooquuxquux'))
Самая быстрая реализация для выполнения нескольких замен строк в Python