Skip to content

Instantly share code, notes, and snippets.

@xfenix
Created May 20, 2013 08:42
Show Gist options
  • Save xfenix/5611090 to your computer and use it in GitHub Desktop.
Save xfenix/5611090 to your computer and use it in GitHub Desktop.
def str_replace_multi(string, replacements, empty_if_list=True):
try:
iterator = repls.iteritems()
except:
if isinstance(replacements, (list, tuple)):
first = iter(replacements)
second = ['']*len(replacements) if empty_if_list else\
first
iterator = dict(zip(first, second))
else:
return string
return reduce(lambda a, kv: a.replace(*kv), iterator, string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment