Judge2020
Judge2020

Reputation: 349

If string contains an list item

I'm looking for a way to check if an string contains anywhere in it, a list item

test = ['he', 'she', 'them']
string = 'hello'
if any(test in s for s in string):
    print 'yes'

I try this, and since 'he' is in 'hello' it should print yes, but i get

TypeError: 'in <string>' requires string as left operand, not list

any help?

Upvotes: 0

Views: 94

Answers (1)

falsetru
falsetru

Reputation: 369454

Instead of iterating a string object, iterate the list object test.

And check whether the test item is in string:

test = ['he', 'she', 'them']
string = 'hello'
if any(test_item  in string for test_item in test):
    print 'yes'

Upvotes: 1

Related Questions