[Solved] Extract single words or phrases from list of strings which are not in base string


Update

This version adds all already seen words to the exclude set:

exclude = set('why kid is upset'.split())
list_of_strings = ['why my kid is upset', 
                   'why beautiful kid is upset', 
                   'why my 15 years old kid is upset',
                   'why my kid is always upset']
res = []
for item in list_of_strings:
    words = item.split()
    res.append(' '.join(word for word in words if word not in exclude))
    exclude.update(set(words))
print(res)

Result:

['my', 'beautiful', '15 years old', 'always']

This would work:

exclude = set('why kid is upset'.split())
list_of_strings = ['why my kid is upset', 
                   'why beautiful kid is upset', 
                   'why my 15 years old kid is upset',
                   'why my kid is always upset']
>>> [' '.join(word for word in item.split() if word not in exclude) for item
     in list_of_strings]
['my', 'beautiful', 'my 15 years old', 'my always']

4

solved Extract single words or phrases from list of strings which are not in base string