Hello everbody!
I would appreciate your help with censor dispenser task, part 3, where you have to censor all words from a list and replace them with allowed terms.
This is the task:
Write a function that can censor not just a specific word or phrase from a body of text, but a whole list of words and phrases, and then return the text.
Mr. Cloudy has asked that you censor all words and phrases from the following list in email_two . Here is my code:

proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]
ok_terms = ["it", "individual pattern", "understanding", "safety", "special feature", "its", "itself"]
def censor_two(email):
for term in proprietary_terms:
if term in email:
email_censored = email.replace(proprietary_terms[proprietary_terms.index(term)], ok_terms[ok_terms.index(term)])
return email_censored
print(censor_two(email_two))

Here is the error message I get:

Traceback (most recent call last):
File "script.py", line 22, in <module>
print(censor_two(email_two))
File "script.py", line 17, in censor_two
email_censored = email.replace(proprietary_terms[proprietary_terms.index(term)], ok_terms[ok_terms.index(term)])
ValueError: 'she' is not in list

Thank you for your answer, stetim94! It makes sense â€śsheâ€ť is not in ok_terms, so that was a problem. But could you please explain, what do you mean by â€śtensor the wordâ€ť? Iâ€™ve corrected that part of the code, which gives the index of the element in ok_terms (it is the same index as of the element from proprietary_terms, so I put the index as [proprietary_terms.index(term)]). Now I donâ€™t get the error message, but the code doesnâ€™t replace words.

proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]
ok_terms = ["it", "individual pattern", "understanding", "safety", "special feature", "its", "itself"]
def censor_two(email):
for term in proprietary_terms:
if term in email:
email_censored = email.replace(proprietary_terms[proprietary_terms.index(term)], ok_terms[proprietary_terms.index(term)])
return email_censored

Un-indent return so that the for loop can complete.

Also, check out the function enumerate(). It would help to simplify your code considerably.

(EDIT) Just noticed: you are continually over-writing email_censored with the latest replacement in email. The idea is to have the replacements be cumulative. This will be complicated by the fact that each replacement changes the indexing.

i am also challenging the censor dispenser at that moment. My code is nearly the same.

So when i am running only the first term in â€śproprietary_termsâ€ť gets censored.
But shouldnâ€™t it work because i run the for loop " term in proprietary_terms:" ?

And if the email_censored is over-written shouldnâ€™t be the last index replaced (â€śherselfâ€ť) printed?
In my case â€śsheâ€ť is replaced

Yes, that is a good observation, that you would expect only the last item to show up. However, if return is indented as we see above, the function will complete after only one trip through the for loop (remember that return halts processing of the function).

Good point! I outdented the return and wich surprise the last string in the list is censored.
Now this makes sense.

But the over-writing of email_censored still is the main issue in solving this.
Nevertheless i trying to solve. You think this is the most elegant way to solve this problem or is this approach not professional?

def censor_email_two(email):
for term in proprietary_terms:
email = email.replace(term, â€ś#â€ť * len(term))
return email
print(censor_email_two(email_two))

but for example in the word researchers its getting censored like researc###s. So i have to tell him he only searches for the words not surrounded by other letters. I will try with something like witespaces before term. Lets see this evening

Your code is different to the one youâ€™re replying to, the names in particular results_lst and email are different. Can you work out why results_lst = email.replace() might behave differently for repeated iterations compared to the previous one?

Since there wasnâ€™t any response Iâ€™ll try to give an answer 3 years later

By copying email to a results_lst weâ€™ll only manipulate the copy and not the original email list.
This is preferred if we need the email list later for further manipulations or you simply donâ€™t want to destroy the original data upon running the function.