How to Check if a substring is a part of List of Strings using Python?


Generally, a string is an immutable data structure which is used to store the data in the string format within the single quotes or within the double quotes. It is immutable which means once the string is created the data in the string can’t be changed but can perform various operations on strings to manipulate the data

A substring is a contiguous sequence of characters within a larger string. It is a smaller part of a string that can be extracted or searched within the original string. For example, let’s consider the string "Hello, World!" as the original string and "Hello", "World", and "!" are all substrings of the original string.

In python we have different approaches for checking if a substring is a part of the list of strings. Let’s see each approach in detail.

Using Loops

This approach involves iterating over each string in the list and checking if the substring is present in each string.

Example

In this example, we loop through each string in the list using a for loop. For each string, we use the in operator to check if the substring is present. If we find a match, we return True, indicating that the substring is present in at least one string. If no match is found in any string, we return False.

def is_substring_present_loop(substring, string_list):
   for string in string_list:
      if substring in string:
         return True
      return False
res = is_substring_present_loop("Happy", ["Happy","learning"])
print(res)

Output

True

Using List Comprehension

The list comprehension approach allows us to create a new list containing Boolean values indicating whether the substring is present in each string. We can then use the any() function to check if any of the Boolean values is True.

Example

In this example, we use a list comprehension to create a new list called match_list. Each element in match_list represents whether the substring is present in the corresponding string. Finally, we use the any() function to check if any element in match_list is True i.e. we return True otherwise, we return False.

def is_substring_present_listcomp(substring, string_list):
   match_list = [substring in string for string in string_list]
   return any(match_list)
res = is_substring_present_listcomp("Happy", ["Happy","learning"])
print(res)

Output

True

Using the any() function and Generator Expression

This approach is similar to the list comprehension approach, but instead of creating a separate list, it uses a generator expression within the any() function directly.

Example

In this example, we are using the generator expression (substring in string for string in string_list) which generates a sequence of Boolean values indicating if the substring is present in each string. The any() function checks if any of these Boolean values is True. If so, it returns True otherwise it returns False.

def is_substring_present_any(substring, string_list):
   return any(substring in string for string in string_list)
res = is_substring_present_any("Tutorialspoint", ["Happy","learning"])
print(res)

Output

False

Updated on: 07-Aug-2023

50 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements