1

A list of original strings:

Originals = ["nice apple", "orange", "pear sweet", "red ape"]

A list of strings to remove:

To_remove = ["nice ", " sweet"]

What I want to achieve is to remove the strings needed to be removed in each elements in the original words

result: ["apple", "orange", "pear", "red ape"]

I do following but it doesn’t produce a good result.

for t in To_remove:
    for o in Originals:
        print o.replace(t, "")

what would be the right way? Thank you.

3
  • 1
    but it doesn’t produce a good result -- What do you see? Please show us! Commented Feb 3, 2017 at 9:04
  • Your questions title is quite confusing, maybe you should reword it to match your actual need. Commented Feb 3, 2017 at 9:27
  • Suggestion for a catching title: Howto "censor" a string list in python? :-) Commented Feb 3, 2017 at 10:12

4 Answers 4

4

Strings are immutable. Hence, none of the methods you can call on a string does an in-place modification. They all return a new string, so you have to reassign the string returned by replace:

for t in To_remove:
    for i in range(len(Originals)):
        Originals[i] = Originals[i].replace(t, "")

You can check out this question for how to merge the replacement of multiple patterns.

Sign up to request clarification or add additional context in comments.

5 Comments

And for just a tip and in case to use PEP8 standards: use snake case.
@Wencakisa see OP (concerning snake case)
the return value -- better use the result of the replace operation or something like this, to make clear what you mean.
@Wolf I added some clarification.
much better, though you provide a link to the docs of version 2, as I said: hard to google this ;)
3

Because string is immutable, you have to reassign the list element.

for t in To_remove:
    for i, o in enumerate(Originals):
        Originals[i] = o.replace(t, "")

print Originals 

hope this helps.

2 Comments

thank you for the teaching. very good! hope you don't mind I choose the first answered for answer...
I like the print Originals idea, this part is missing in the OP
1

You are printing the results of the replacement, but do not change the list contents. If you have a closer look on the replace method (for strings(!)), you'll see that you are not only not changing the list but also not changing the strings you are getting for o in Originals. I omit including a working example, since schwobaseggl and thangtn [1] already provide it.


[1] Who was really first? The SO timestamps contradict my personal experience.

1 Comment

BTW: the official docs are very sparse, maybe this is the reason you have so bad Google results for it.
1
Originals = ["nice apple", "orange", "pear sweet", "red ape"]
To_remove = ["nice ", " sweet"]
result = []

for o in Originals:
    for t in To_remove:
        result.append(o.replace(t, ""))

print result

Is that what you need?

2 Comments

Some more text may help to understand the purpose of the repetition of the already known.
...I have to correct myself about this (concerning 'repetition') you don't an in-place replacement. A valid option.

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.