You can use itertools.product
like this
>>> list1 = ['abra', 'hello', 'cfre']
>>> list2 = ['dacc', 'ex', 'you', 'fboaf']
>>> ttext = 'hello how are you?'
>>> from itertools import product
>>> for word1, word2 in product(list1, list2):
... print word1, word2, (word1 in ttext and word2 in ttext)
...
abra dacc False
abra ex False
abra you False
abra fboaf False
hello dacc False
hello ex False
hello you True
hello fboaf False
cfre dacc False
cfre ex False
cfre you False
cfre fboaf False
itertools.product
computes the cartesian product of the iterables passed to it. Now, we check if all the items in product appears in the ttext
.
You can actually make it generic like this
>>> ttext = 'hello how are you?'
>>> for words in product(list1, list2):
... print(words, all(word in ttext for word in words))
The all
function will return True
only when all the elements of the iterable passed to it are Truthy. In your case, it just checks if each of the words in the words
tuple is present in ttext
.