okay here is the example:
data = ['This', 'is', 'a', 'test', 'of', 'the', 'list']
replaceText = 'test'
replaceData =['new', 'test']
i did data.replace(replaceText, replaceData) but it doesn’t work. How to replace a string in a list of string with a list of strings? Any help will be appreciated.
Edit:
The exact condition is to replace or split the words that contain “s” so I put a loop in it. So the end result will print
data = [‘Thi’, ‘i’, ‘a’, ‘te’,’t’, ‘of’, ‘the’, ‘li’,’t’]
In a list, find the position of text with
.index(), then replace by using slice assignment:This will replace only one occurrence of
replaceTextat a time. Demo:To replace all occurences, use
posplus the length ofreplaceDatato skip searching past the previous match:If you need to loop over
datawhile modifying it, use a copy instead: