What is the best way to perform substitutions with re.sub given a list? For example:
import re
some_text = 'xxxxxxx@yyyyyyyyy@zzzzzzzzz@'
substitutions = ['ONE', 'TWO', 'THREE']
x = re.sub('@', lambda i: i[0] substitutions.pop(0), some_text) # this doesn't actually work
The desired output would be:
some_text = 'xxxxxxxONEyyyyyyyyyTWOzzzzzzzzzTHREE'
You just have a syntax error in your lambda:
If you don’t want to modify the list, you can wrap it an iterable.