Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading