Reputation: 349
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
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