Check if all elements of List are in string in Python

This tutorial will discuss about unique ways to check if all elements of list are in string in Python.

Table Of Contents

Technique 1: Using all() function

The all() method accepts a sequence as an argument, and returns True if all the elements in the sequence evaluates to True.

So, we can use the all() method to check if all the strings in a List exists in a given string or not. Steps are as follows,

  • Use a for loop in generator expression to loop over all elements of list.
  • During iteration, for each element in list, check if it exists in a given string, and create a bool list from result values.
  • Use all() method to check if all the values in bool list are True.

Like this,

all(elem in strValue for elem in listOfStrings)

It will return True, if all the strings in list exists in another string, otherwise it will return False.

Let’s see the complete example,

# Example 1
strValue = 'This a sample text for testing'

listOfStrings = ['sample', 'for', 'This']

# Check if all elements of list are in string
if all(elem in strValue for elem in listOfStrings):
    print("Yes, all elements of list are in string")
else:
    print("No, all elements of list are not in string")

# Example 2
listOfStrings = ['sample', 'like', 'This']

# Check if all elements of list are in string
if all(elem in strValue for elem in listOfStrings):
    print("Yes, all elements of list are in string")
else:
    print("No, all elements of list are not in string")

Output

Yes, all elements of list are in string
No, all elements of list are not in string

Technique 2: Using for-loop

Loop over all the strings in a List using a for loop. During iteration, for each string, check if it exists in a given string or not. As soon as, a list element is encountered that does not exist in the given string, break the loop, and mark that all the strings in list does not exists in the given string. We have created a function for this,

def all_in_string(listObj, strValue):
    ''' Returns True if all elements of list
        are in the given string'''
    result = True
    for elem in listObj:
        if elem not in strValue:
            result = False
            break
    return result

It accepts a List and a string as arguments, and returns True if all the strings in List exist in another string.

Let’s see the complete example,

def all_in_string(listObj, strValue):
    ''' Returns True if all elements of list
        are in the given string'''
    result = True
    for elem in listObj:
        if elem not in strValue:
            result = False
            break
    return result

# Example 1
strValue = 'This a sample text for testing'

listOfStrings = ['sample', 'for', 'This']

# Check if all elements of list are in string
if all_in_string(listOfStrings, strValue):
    print("Yes, all elements of list are in string")
else:
    print("No, all elements of list are not in string")

# Example 2
listOfStrings = ['sample', 'like', 'This']

# Check if all elements of list are in string
if all_in_string(listOfStrings, strValue):
    print("Yes, all elements of list are in string")
else:
    print("No, all elements of list are not in string")

Output

Yes, all elements of list are in string
No, all elements of list are not in string

Summary

We learned about two ways to check if all elements of a List exists in another string in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top