You just have a syntax error in your lambda:
>>> substitutions = ['ONE', 'TWO', 'THREE'] >>> re.sub('@', lambda _: substitutions.pop(0), some_text) 'xxxxxxxONEyyyyyyyyyTWOzzzzzzzzzTHREE'
If you do not want to modify the list, you can wrap it iterable.
>>> substitutions = ['ONE', 'TWO', 'THREE'] >>> subs = iter(substitutions) >>> re.sub('@', lambda _: next(subs), some_text) 'xxxxxxxONEyyyyyyyyyTWOzzzzzzzzzTHREE'
source share