Python code Snippet

How to know if a word is inside of a string

This question come up in StackOverflow these days.

Some user is trying to find a word withing a string. Then, he want to use a list of words to find all of them in a list of strings.

My snippet code an easy way is written below:

some_list = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]

sentence_list = ["i'm going this friday", "i'm not going", "i plan to go saturday"]

new_list = []

for day in some_list:
    for sentence in sentence_list:
        if day in sentence:
            new_list.append(sentence)

print(new_list)

$ python lookingwordinlist.py

[“i’m going this friday”, ‘i plan to go saturday’]

If you think you can use this for other uses, let me know!

Download snippet here

Next steps

By using numpy.unique we can reduce the list if one of the sentences is repeated.