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,237
  • 2
  • 17
  • 32
Pat
  • 1,237
  • 3
  • 16
  • 35

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
  • 43,220
  • 16
  • 78
  • 90
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,237
  • 2
  • 17
  • 32
  • 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