0

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?

1
  • 1
    you're writing "if any <list> in <string>", and your error is telling you this Commented Feb 14, 2015 at 14:33

1 Answer 1

1

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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.