1

I want to check if all words are found in another string without any loops or iterations:

a = ['god', 'this', 'a']

sentence = "this is a god damn sentence in python"

all(a in sentence)

should return TRUE.

1

2 Answers 2

4

You could use a set depending on your exact needs as follows:

a = ['god', 'this', 'a']
sentence = "this is a god damn sentence in python"

print set(a) <= set(sentence.split())

This would print True, where <= is issubset.

Sign up to request clarification or add additional context in comments.

1 Comment

You could drop the ' ' to become set(sentence.split())
3

It should be:

all(x in sentence for x in a)

Or:

>>> chk = list(filter(lambda x: x not in sentence, a)) #Python3, for Python2 no need to convert to list
[] #Will return empty if all words from a are in sentence
>>> if not chk:
        print('All words are in sentence')

1 Comment

That's part of all expression...nothing to do about it, but I don't believe that's part of OP concern...as he is avoiding any explicit for loop

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.