I have several lines of a file that I'm looping through and have stored as strings and I'm looking to perform a simple search and replace in each line using either the method built into python strings str.replace() or using regular expressions re.sub() but using a list as argument for the old substring. I know the format usually goes as follows:
string.replace('oldsubstring','newsubstring')
However, if I have a list of strings: ['word1', 'word2', 'word3'], is it possible to use this as the oldsubstring argument so that if any of the elements in the list are found in string, that element is replaced with newsubstring. I know this is possible using a double nested for loop that loops through all of my lines and my list of strings, but I'm looking for a more efficient algorithm to accomplish this.
Follow Up Question:
Another problem I have found is that there are times where my list of strings will look like:
['word1', 'word1_suffix', 'word2', 'word3'] NOTE: Order of these elements is not guaranteed to be the same each run.
When using the double nested for loop method, if word1_suffix appears in the current line I'm looking at, and I then loop through my list of strings, if word1 happens to appear in my list of strings first, the replacement will be newsubstring_suffix rather than replacing the entire substring: word1_suffix with newsubstring.
NOTE: I know that using a regular expression I can ensure that word1_suffix is it's own full word surrounded by spaces, but that are times where I do want a part of my line that follows the format: word1_miscellaneous to be replaced as newsubstring_miscellaneous so that method will not entirely solve my problem.