1

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']

3 Answers 3

3

In a list, find the position of text with .index(), then replace by using slice assignment:

pos = data.index(replaceText)
data[pos:pos+1] = replaceData

This will replace only one occurrence of replaceText at a time. Demo:

>>> data = ['This', 'is', 'a', 'test', 'of', 'the', 'list']
>>> replaceText = 'test'
>>> replaceData =['new', 'test']
>>> pos = data.index(replaceText)
>>> data[pos:pos+1] = replaceData

To replace all occurences, use pos plus the length of replaceData to skip searching past the previous match:

pos = 0
while True:
    try:
        pos = data.index(replaceText, pos)
    except ValueError:
        break
    data[pos:pos+1] = replaceData
    pos += len(replaceData)

If you need to loop over data while modifying it, use a copy instead:

for n in data[:]:
    # maniplate data
Sign up to request clarification or add additional context in comments.

5 Comments

actually the condition has made me to loop data. i posted the code above. would your method still work?
Your loop makes no sense. You cannot loop over data and change it in-place, for example. Do you mean that you'll replace multiple things at a time?
yah, the actually condition needs me to replace or split any string that contains the character 's', say 'test' will be replaced by 'te' and 't' to the list.
also "list" will be split in to "li" and "t" and replaced to the list.
@tipsywacky I've added an answer that does that (if I'm understanding correctly) - you may wish to update the question
2

You can use list's index() method to find the position p of replaceText:

p = data.index(replaceText)

and then use the construct

data[start:end] = another_list

to replace elements from p to p+1 (end is not inclusive) with replaceData:

data[p:p+1] = replaceData

Note that index() throws ValueError if replaceText does not exist in data:

try:
  p = data.index(replaceText)
  data[p:p+1] = replaceData
except ValueError:
  # replaceText is not present in data, handle appropriately.

Comments

1

yah, the actually condition needs me to replace or split any string that contains the character 's', say 'test' will be replaced by 'te' and 't' to the list

from itertools import chain
data = ['This', 'is', 'a', 'test', 'of', 'the', 'list']
>>> filter(None, chain.from_iterable(el.split('s') for el in data))
['Thi', 'i', 'a', 'te', 't', 'of', 'the', 'li', 't']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.