Skip to content
Advertisement

How to test for substrings in the list

I need to check if a string is in the list my_list.

my_list = ['word1,word2,word3', 'g1,g2', 'word1']

When I run 'word2' in my_list, it returns False instead of True.

How can I test for substrings in the list?

Advertisement

Answer

You have to check whether your string is part of each list element.

Something like this:

for elem in my_list:
    if 'word_2' in elem: return True
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement