2

So very much like in this question except that I would like to replace any part of words that matches with anything in error_list with ''.

error_list = ['[br]', '[ex]', 'Something']
words = ['how', 'much[ex]', 'is[br]', 'the', 'fish[br]', 'noSomething', 'really']

The desired output would be

 words = ['how', 'much', 'is', 'the', 'fish', 'no', 'really']

My vain attempt was,

words = [w.replace(error_list , '') for w in word]

EDIT: Maybe I should also say that I have done this with loops but was looking for a more pythonic way.

Community
  • 1
  • 1
Little Bobby Tables
  • 4,466
  • 4
  • 29
  • 46
  • 1
    Googling your question's exact title produced some good information (the linked target was the first hit). – TigerhawkT3 Nov 06 '15 at 10:07
  • The answer on "Python replace multiple strings" would certainly work but it is maybe a little too powerful. Ultimately, I would have to create a dict with my list as the keys and with `""` as the values. – Little Bobby Tables Nov 06 '15 at 10:20

1 Answers1

1
error_list = ['[br]', '[ex]', 'Something']
words = ['how', 'much[ex]', 'is[br]', 'the', 'fish[br]', 'noSomething', 'really']   

for j in error_list:
    for index, i in enumerate(words):
            if(j in i):
                    i1=i.replace(j,"")
                    words[index]= i1

output

['how', 'much', 'is', 'the', 'fish', 'no', 'really']

See it in action

Ravichandra
  • 2,162
  • 4
  • 24
  • 36