-2

I have a list of strings like this:

string_list=["abcdefg","qwrbcdqqq","ophdkjhfkbcd","jdfkvndn"]

Notice that a word exists in some of them (here bcd) but the location of this word is not fixed and changes in each string. Now how do I remove this word from those strings if I know what the word is?

Edit: Target list is:

target_list=["aefg","qwrqqq","ophdkjhfk","jdfkvndn"]
Gudarzi
  • 486
  • 3
  • 7
  • 22
  • Please clarify your desired output; two of us got different ideas. – Prune Aug 07 '18 at 16:32
  • I agree this is ambiguous - I read the request as substring removal but it could equally be whole string removal. – AChampion Aug 07 '18 at 16:33
  • Do you mean word, or letters? – Alexander Aug 07 '18 at 16:34
  • @AChampion: On re-reading this, I hope you're correct from a linguistic standpoint; but if you are, then this shouldn't have been posted in the first place. – Prune Aug 07 '18 at 16:35
  • Down-votes for the question and up-votes for answers! Please tell me what's wrong with my question that needs down-voting? – Gudarzi Aug 07 '18 at 16:42

3 Answers3

4

You can iterate over the list and .replace('bcd', ''), e.g.:

In []: [s.replace('bcd', '') for s in string_list]
Out[]: ['aefg', 'qwrqqq', 'ophdkjhfk', 'jdfkvndn']
AChampion
  • 29,683
  • 4
  • 59
  • 75
2
string_list = [temp.replace('bcd','') for temp in string_list]
mad_
  • 8,121
  • 2
  • 25
  • 40
1

Make a new list of those words which do not include the target substring.

target = "bcd"
new_list = [word for word in string_list if not target in word]
Prune
  • 76,765
  • 14
  • 60
  • 81