1

I want to make replace of all values that are in list(i) for a string (li) and return final string with replaced values.

Instead, my code replaces only one value at a time. Is there a way to fix this simple for loop?

li = str('Text 1. Text 2? Text 3!')
i = ['.', '?', '!']

for y in i:
    t = li.replace(y,'')
print(t)

5 Answers 5

2

You need to re-assign to li each time, kind of recursive replacement. Because for now you always go from original li to t with a different replacement letter

li = 'Text 1. Text 2? Text 3!'
i = ['.', '?', '!']
for y in i:
    li = li.replace(y, '')

You can also use regex module re and pattern [.?!]

li = 'Text 1. Text 2? Text 3!'
i = ['.', '?', '!']
li = re.sub("[" + "".join(i) + "]", '', li)
Sign up to request clarification or add additional context in comments.

2 Comments

I have another question as follow up to working with that splited and further modified list. I guess I better start a new thread.
@АртёмФомичёв Yes you should ;)
0

You have to update the value of li everytime replace method is used

li = 'Text 1. Text 2? Text 3!'
i = ['.', '?', '!']

for y in i:
    li = li.replace(y, '')

print(li)

this way the loop checks for the first string to replace then re-assigns the new li value to li and then checks for next string to replace.

Comments

0

The problem is that you are replacing sub strings in li but assigning the returned value to t. In theory, t is always overwritten when you assigned the replaced string to t and li remains the same because you never assigned the replaced string to li

So change the code to this

li = str('Text 1. Text 2? Text 3!')
i = ['.', '?', '!']

for y in i:
    li = li.replace(y,'')
print(li)

4 Comments

@azro. I know. I just wanted to explain why does it happens so. But yours is the original and the correct one
Thank you, Sujay. It makes sense why it didn't work. I have another question. I replaced 'replace' function with 'split' to see if it works, however there was an error of list object has no attribute split. Is there a way to get around with it and use "split" function and for loop here?
@АртёмФомичёв You cannot invoke .split method on a list. The problem here is .split method converted li to lists which was reassigned to it.
I figured it out. In a string just replaced all values of i with "+". Then split by that "+".
0

I think you should use filter in this case, like this:

li = 'Text 1. Text 2? Text 3!'
t = ''.join(list(filter(lambda x: x not in '.?!', li)))
print(t)

Comments

0

U can try this:

li = 'Text 1. Text 2? Text 3!'
i = ['.', '?', '!']
t = li
for y in i:
    t = t.replace(y, '')
print(t)

Value of li remains as it was. There is no need to use str function. Output:

Text 1 Text 2 Text 3

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.