Feature Proposal: Multi-String Replacement Using a Dictionary in the .replace() Method

By analogy with .format and .format_map, this could be called .replace_map.

Historically, this couldn’t be done because the order of application in the presence of overlapping patterns (or patterns that included later patterns in their output) would have been unpredictable when passing a built-in dict.

These days, dicts are insertion ordered, so the method can safely be defined as equivalent to:

modified = original
for k, v in replacements.items():
    modified = modified.replace(k,v)
5 Likes