I am trying to remove a list of words from a list of text but output seems like its not getting removed. Please help me in the removing the text from the list
text_list = ['apple is good for health', 'orange and grapes are tasty']
words = ['apple','orange','grapes']
words_format = r'\b(?:{})\b'.format('|',join(words))
remove_words = lambda y: y.replace(words_format,' ')
new_text = list(map(remove_words, text_list))
Expected output:
['is good for health', 'and are tasty']
>Solution :
I would just split the input, filter out the invalid words and then join the results again:
[" ".join([word for word in text.split(" ") if word not in words]) for text in text_list]