10/15 Censor


Here is my code:

def censor(text, word):
  x = text.split()
  for i in x:
    if i == word:
      x.append('*' * len(word))
  text = " ".join(x)
  print text
censor('yes yes yes yes', 'yes')

My code returns: “yes yes *** ***”

Question: Why are the last two items in list x being iterated but not the rest? I was thinking that the “for i in x:” action iterated through all of the indexes in x. Any help is much appreciated!


.append() is inserting at the end, what if you have something like:

censor('this is a problem', 'is')

then is gets censored and appended to the very end, the sentence should keep the same order of words.

remove is challenging, lists can’t have empty indexes the moment you remove an item from the list everything to the right of the removed item moves one position to the left. Combine this with the loop you are using, the item after the removed item gets skipped


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.