Ethan San Jose
Ethan San Jose

Reputation: 27

Using .replace() with a List in python

I'm trying to get python to read a txt file and then replace certain words in it to "XXXX" I have a list that contains all of the words but the problem is return doesn't accept lists! I have tried doing

def email2(email):

  proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]

  for words in email:

    if "she" in email:
      replaced = email.replace("she", "XXXX")
      return replaced

    if "personality matrix" in email:
      replaced = email.replace("personality matrix", "XXXX")
      return replaced

    if "sense of self" in email:
      replaced = email.replace("sense of self", "XXXX")
      return replaced

    if "self-preservation" in email:
      replaced = email.replace("self-preservation", "XXXX")
      return replaced

    if "learning algorithm" in email:
      replaced = email.replace("learning algorithm", "XXXX")
      return replaced

    if "her" in email:
      replaced = email.replace("her", "XXXX")
      return replaced

    if "herself" in email:
      replaced = email.replace("herself", "XXXX")
      return replaced
    

print(email2(email_two))

and this works and does what I need it to (with a bit of tinkering) but as you can see, the code is unnecessarily WAY TOO LONG and I was hoping if any of you could teach me a way I could make it more compact if only I could get all the words in the list proprietary terms to be replaced without having to type all the words in a if statement one by one

Upvotes: 0

Views: 86

Answers (4)

Gil Pinsky
Gil Pinsky

Reputation: 2493

Another (shorter) way to do it using the re package:

import re
def check_email(email):
    proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]
    return re.sub('|'.join(proprietary_terms),'XXXX',email)

Upvotes: 1

Gavin Wong
Gavin Wong

Reputation: 1260

def check_email(email):

    proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]

    for word in proprietary_terms:
        if word in email:
            email.replace(word, "XXXX")

    return email

Upvotes: 1

Konrad Rudolph
Konrad Rudolph

Reputation: 545865

Don’t loop over characters in the email (if email is of type str, then word is a character, not a word!) — loop over the proprietary terms.

Your code is also returning prematurely, it will never replace more than one word. And lastly, the if … in email check is redundant, leave it out.

def email2(email):
  proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]

  for word in proprietary_terms:
    email = email.replace(word, "XXXX")

  return email

Upvotes: 2

DirtyBit
DirtyBit

Reputation: 16782

You can iterate over the proprietary_terms list and check if the word in the list exists in the email, if it does, you can replace it:

proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]

for word in proprietary_terms:
    if word in email:
        email = email.replace(word, "XXXX")
return email

Upvotes: 1

Related Questions