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

Check to see if text is contained within a variable in a list in Python

I am working with this code:

test_list = ['small_cat', 'big_dog', 'turtle']

if 'dog' not in test_list:
    output = 'Good'
else:
    output = 'Bad'

print (Output) 

Because ‘dog’ is not in the list, ‘output’ will come back with a response of ‘Good’. However, I am looking for ‘output’ to return ‘Bad’ because the word ‘dog’ is part of an item in the list. How would I go about doing this?

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 should iterate all the values in test_list

output = 'Good'
for test_word in test_list:
    if 'dog' in test_word:
        output = 'Bad'
        break

print(output)
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