Removing list of words from a string

This is one way to do it:

query = 'What is hello'
stopwords = ['what', 'who', 'is', 'a', 'at', 'is', 'he']
querywords = query.split()

resultwords  = [word for word in querywords if word.lower() not in stopwords]
result=" ".join(resultwords)

print(result)

I noticed that you want to also remove a word if its lower-case variant is in the list, so I’ve added a call to lower() in the condition check.

Leave a Comment