Check if a substring is in list of strings in Python

In this python tutorial, you will learn how to check if a substring is in a list of strings.

Table Of Contents

Suppose we have a list of strings like,

['welcome', 'to', 'thisPointer', 'Python', 'CPP']

Now we want to check if a given string like “this” is a substring of any string in the list. There are different ways to do this. Let’s discuss them one by one.

Check if a substring is in a list of strings using any()

The any() function will return True if there is at least one value present in the given boolean list. Here, we will iterate the list of strings inside a for loop to check whether the given substring is present or not in any of the string. It will return a boolean list, where each True value denotes that corresponding string in the original list has the given substring. Finally, we are applying any() to it. If there is any True value in the boolean list, then it will return True. It means substring is present in the list of strings.

Advertisements

Syntax:

any(input_str in iterator for iterator in inp_list)

Where,

  • input_str is the input substring and input_list is the list that holds strings separated by a comma.
  • Returns True if any string in list contains the given substring input_str.

Example 1:

We will create a list with five strings and check for substring – ‘this’ in that list.

# Consider the list
inp_list = ["welcome", "to", "thisPointer","Python","CPP"]

# The substring that needs to be checked
input_str='this'

# Check substring is present in the above list of strings
result = any(input_str in i for i in inp_list)

if result:
    print('List contains the given substring.')
else:
    print('List does not contains the given substring.')

Output:

List contains the given substring.

‘this’ is present in the list. Hence it returned True.

Example 2:

We created 5 strings in a list and check for substring-‘html’ in that list.

# Consider the list
inp_list = ["welcome", "to", "thisPointer","Python","CPP"]

# The substring that needs to be checked
input_str='html'

# Check substring is present in the above list of strings
result = any(input_str in i for i in inp_list)

if result:
    print('List contains the given substring.')
else:
    print('List does not contains the given substring.')

Output:

List does not contains the given substring.

The substring ‘html’ is not present in the list. Hence it returned False.

Check if a substring is in a list of strings using join()

We will specify the substring and check if it is present in the list or not used in the membership operator. For this, we will join all the strings in a string using join() with a separator – \t

Syntax:

input_str in '\t'.join(inp_list)

Where,

  1. The input_str is the input string and input_list is the list that holds a set of strings separated by a comma.
  2. the ‘\t’ separator that separated each string after joining.

Example 1:

We will create a list with 5 strings and check for substring – ‘to’ in that list.

# Consider the list
inp_list = ["welcome", "to", "thisPointer","Python","CPP"]

# The substring that needs to be checked
input_str='to'

# Check substring is present in the above list of strings
result = input_str in '\t'.join(inp_list)

if result:
    print('List contains the given substring.')
else:
    print('List does not contains the given substring.')

Output:

List contains the given substring.

The substring ‘to’ is present in the list. Hence it returned True.

Example 2:

We will create a list with 5 strings and check for substring-‘html’ in that list.

# Consider the list
inp_list = ["welcome", "to", "thisPointer","Python","CPP"]

# The substring that needs to be checked
input_str='html'

# Check substring is present in the above list of strings
result = input_str in '\t'.join(inp_list)

if result:
    print('List contains the given substring.')
else:
    print('List does not contains the given substring.')

Output:

List does not contains the given substring.

The substring ‘html’ is not present in the list. Hence it returned False.

Check if a substring is in a list of strings using list comprehension

We will iterate the over the strings in list and check if the input string is present in the list or not. If the substring is present in the list, it returns True, otherwise False is returned.

Syntax:

[iterator for iterator in inp_list if(iterator in input_str)]

Where,
1. The input_str is the input string and input_list is the list that holds a set of strings separated by a comma.
2. The iterator is used to iterate the strings in a list inside for loop.
3. Filter elements in list and create a new list with only those strings that contains the substring.
4. If new list is empty then it means no string contains the given substring.
5. Pass the list to bool(). If list is empty then it will False else return True.

Example 1:

We will create a list with 5 strings and check for substring-‘to’ in that list.

# Consider the list
inp_list = ["welcome", "to", "thisPointer","Python","CPP"]

# The substring that needs to be checked
input_str='to'

# Check substring is present in the above list of strings
result = bool([elem for elem in inp_list if input_str in elem])

if result:
    print('List contains the given substring.')
else:
    print('List does not contains the given substring.')

Output:

List contains the given substring.

The substring ‘to’ is present in the list. Hence it returned True.

Example 2:

We will create a list with 5 strings and check for substring-‘html’ in that list.

# Consider the list
inp_list = ["welcome", "to", "thisPointer","Python","CPP"]

# The substring that needs to be checked
input_str='html'

# Check substring is present in the above list of strings
result = bool([elem for elem in inp_list if input_str in elem])

if result:
    print('List contains the given substring.')
else:
    print('List does not contains the given substring.')

Output:

List does not contains the given substring.

The substring ‘html’ is not present in the list. Hence it returned False.

Summary

We have seen four methods to check if a substring is in a list of strings. Mostly, we used for loop to iterate over a list of strings and checked for the substring.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

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

Scroll to Top