Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
is there a way to do something like this?
if ['hel','ell','orl'] in 'hello world' :
I want to see if all of these strings occur in the word. If possible in a shorter way than completely writing a multiline foor loop.
You could do:
if all( x in 'hello world' for x in ['hel','ell','orl'] ): print "Found all of them"
The built-in functions all and any are useful for this kind of thing.
all
any
Add a comment
if all(substr in 'hello world' for substr in ('hel','ell','orl')): # all are contained
The advantage of all() is that it stops checking as soon as one substr does not match.
all()
substr
A multiline for loop is the correct way to proceed. If you don't like how it looks in your code extract it into a function and then you just have a single line function call for the same thing.
for
Required, but never shown
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.
Explore related questions
See similar questions with these tags.