Home > Net >  How to test for substrings in the list
How to test for substrings in the list

Time:06-28

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?

CodePudding user response:

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
  • Related