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.

Iron Fist
  • 10,739
  • 2
  • 18
  • 34
Pat
  • 1,299
  • 4
  • 17
  • 40

2 Answers2

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.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
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')
Iron Fist
  • 10,739
  • 2
  • 18
  • 34
  • 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 – Iron Fist Feb 10 '16 at 14:47