1

I would like to know the quickest or the shortest way to check if all the strings in a list occur in another specific string.
Ex:

l = ['I','you']
s = ['I do like you']

in this case, I would like to see if both I and you appear in I do like you. Is there a one-liner? Instead of a for loop and checking one by by manually, in a traditional way?

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
Huzo
  • 1,652
  • 1
  • 21
  • 52
  • 1
    `all(w in s for w in l)`? Assuming `s` is actually the string `'I do like you'`, and not a list with the string. If `s` is a list of strings and you want to check against all of them then `all(w in t for w in l for t in s)`. – jdehesa May 16 '19 at 09:49
  • Possible duplicate of [Check if multiple strings exist in another string](https://stackoverflow.com/questions/3389574/check-if-multiple-strings-exist-in-another-string) – Sheldore May 16 '19 at 09:57

3 Answers3

4

Use all() which returns a True if all elements of the iterable are truthy or else False:

all(x in s[0] for x in l)

In code:

l = ['I','you']
s = ['I do like you']

print(all(x in s[0] for x in l))
# True
Austin
  • 25,759
  • 4
  • 25
  • 48
2

You can use all() operator, which returns True if every element of an Iterator is True or if it is empty.

l = ['I', 'you']
s = 'I do like you'
print(all(x in s for x in l))

You might be intereseted by the any() operator, which returns True if at least one element is True.

BlueSheepToken
  • 5,751
  • 3
  • 17
  • 42
1

I guess you want the words and not just strings. For this use:

all(_ in s[0].split() for _ in l)

Ricardo Branco
  • 5,740
  • 1
  • 21
  • 31