python

Check if a substring exists in a list of strings Python

A substring is a string that is a part of a larger string. A string is a sequence of characters. A substring may occur multiple times within a string. In this post, we will explain multiple methods to check if a substring exists in a list of strings using Python code examples.

Solution 1: Using join() and 'in' keyword

The join() method is used to concatenate a list of strings into a single string. The in keyword is used to check if a substring exists in a string. We can use these two methods to check if a substring exists in a list of strings.

cars_list = ["Toyota", "Tesla", "Ford", "Volvo"]

cars_str = "\t".join(cars_list)

if ('ord' in cars_str):
    print("Substring exists in the list")
else:
    print("Substring does not exist in the list")

Output

Substring exists in the list

Explanation of the above code example line by line

  1.  The first line creates a list called cars_list.
  2. The second line creates a string called cars_str by joining the elements of cars_list together with a tab character.
  3. The third line checks to see if the substring 'ord' exists in cars_str.
  4. The fourth line prints 'Substring exists in the list' if 'ord' is in cars_str.
  5. The fifth line prints 'Substring does not exist in the list' if 'ord' is not in cars_str.

Enclose the above code in a function and test for multiple substrings

def is_substring_exist(my_list, sub_str):
    joined_str = "\t".join(my_list)
    if (sub_str in joined_str):
       return True
    else:
        return False

lst = ["Apple", "Banana", "Papaya"]

print(is_substring_exist(lst, "ana")) # -> True

print(is_substring_exist(lst, "les")) # -> False

Solution 2:  Using any() function

Python has a built-in function called any() which can be used to check if a substring exists in a list of strings. any() is a built-in function that returns True if any element of an iterable is True. If all elements are false, it returns False.

The syntax of any() is:

any(iterable)

where iterable is an iterable object (list, tuple, dictionary, set, etc.).

Code example

lst = ["Apple", "Banana", "Papaya"]

substr_exist = any("pp" in sub for sub in lst)

if substr_exist:
    print("Substring exist")
else:
    print("Substring does not exist")

Output

Substring exist

In this code, we create a list called lst that contains the strings "Apple", "Banana", and "Papaya". Then, we use the any() function to check if any of the substrings in the list contain the string "pp". If any of the substrings do contain "pp", the substr_exist variable will be set to True, and the code will print "Substring exist". If none of the substrings contain "pp", the substr_exist variable will be set to False, and the code will print "Substring does not exist".

Solution 3: Check substring in a list using the next() function

If you want to check if a particular substring exists in a list of strings, you can do so using the next() function. This function will return the next element in the list that contains the substring you are looking for.

Here we will show you how to return True if the substring matched in the list of strings and False if the substring does not exist in the list.

Code example

names_list = ["John", "Mohit", "Sumit", "Sonia"] 
q_str = "mit"

substr_exist = next((True for name in names_list if q_str in name), False)

print(substr_exist)

Output

True

Explanation of the above code example

  1. This code is checking if a substring exists in a list of strings. The first line creates a list of strings variable called 'names_list'.
  2. The second line creates a string variable called q_str.
  3. The third line uses a generator expression to check if q_str is a substring of any of the strings in the list. If it is, the generator expression will return True. Otherwise, it will return False.
  4. The fourth line prints the value of substr_exist.

Get the item that contains the substring in the list

This program will return the item in the list of strings that contains the substring that is inputted. This is useful if you have a list of strings and you want to find a specific string in the list. Check the below Python program.

names_list = ["John", "Mohit", "Sumit", "Sonia"] 
q_str = "mit"

result = [name for name in names_list if q_str in name]

print(result)

Output

['Sumit']

This code example is using a list comprehension to create a new list, result, from the names_list. The list comprehension is checking to see if the string, q_str, is a substring of each name in the names_list. If it is, then that name is added to the new list, result.

Was this helpful?