0

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?

Fluxy
  • 2,838
  • 6
  • 34
  • 63
  • 4
    Does this answer your question? [How to check if a string is a substring of items in a list of strings?](https://stackoverflow.com/questions/4843158/how-to-check-if-a-string-is-a-substring-of-items-in-a-list-of-strings) – luk2302 Jun 28 '22 at 09:30
  • or maybe this one? [Does Python have a string 'contains' substring method?](https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method) – Felix Jun 28 '22 at 09:31

1 Answers1

1

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
B1TC0R3
  • 131
  • 8