Check if List contains a substring in Python

In this article, we will discuss different ways to check if a list contains a substring or not in Python.

Table Of Contents

Introduction

Suppose we have a list of strings. Something like this,

["why", "where", "how", "this", "what"]

Now we want to check if any string in this list contains a substring – “is” or not. Here, the string “this” in list contains the substring “is”, so yes this list contains the given substring.

There are different ways to check if list contains a substring or not. Let’s discuss them one by one.

Advertisements

Check if list contains a substring using any() function

Iterate over all strings in list, and for each string, check if it contains the given substring or not. If a string contains the given substring then the conditional expression will evaluate to True, otherwise it will evaluate to False. Pass all the resultant values to the any() function, to check if any expression is evaluating to True or not.

The any() function in Python, accepts a sequence of boolean values, and returns True if any value in this boolean list evaluates to True. So, in this case if any() function returns True, it means list has a string that contains the given substring.

Let’s see an example. Here, we will check if any string in this list contains a substring – “is” or not.

# List of strings
listOfStrs = ["why", "where", "how", "this", "what"]

# A Substring
subStr = "is"

# check if any string in list contains the given substring 
if any(subStr in elem for elem in listOfStrs):
    print('Yes, List contains the substring')
else:
    print('No, List does not contains the substring')

Output :

Yes, List contains the substring

Yes, there is atleast a string in the list that contains the given substring.

Check if list contains a substring using List comprehension

Filter the list using List comprehension, and select only those strings which contains the given substring. If the length of this filtered list is more than 0, then it means that list has a string that contains the given substring. Let’s see an example. Here, we will check if any string in this list contains a substring – “is” or not.

# List of strings
listOfStrs = ["why", "where", "how", "this", "what"]

filteredList = [elem for elem in listOfStrs if "is" in elem]

# check if any string in list contains the given substring 
if len(filteredList) > 0:
    print('Yes, List contains the substring')
else:
    print('No, List does not contains the substring')

Output :

Yes, List contains the substring

Yes, there is atleast a string in the list that contains the given substring.

Check if list contains a substring using a For Loop

Iterate over all the strings in a list using a for loop, and for each string, check if the given substring exists in that string or not. As soon as, it finds a string like that, break the loop. Let’s see an example. Here, we will check if any string in this list contains a substring – “is” or not.

# List of strings
listOfStrs = ["why", "where", "how", "this", "what"]

# A Sub String
subStr = "is"

result = False

# check if any string in list contains the given substring 
for elem in listOfStrs:
    if subStr in elem:
        result = True
        break

if result:
    print('Yes, List contains the substring')
else:
    print('No, List does not contains the substring')

Output :

Yes, List contains the substring

Yes, there is atleast a string in the list that contains the given substring.

Check if list contains a substring using filter() and Lambda functions

Using the filter() function, and a lambda expression, select only those strings which contains the given substring. If the length of this filtered list is more than 0, then it means that list has a string that contains the given substring. Let’s see an example. Here, we will check if any string in this list contains a substring – “is” or not.

# List of strings
listOfStrs = ["why", "where", "how", "this", "what"]

# A Sub String
subStr = "is"

# check if any string in list contains the given substring 
filteredList = list(filter(lambda elem: subStr in elem, listOfStrs))

if len(filteredList) > 0:
    print('Yes, List contains the substring')
else:
    print('No, List does not contains the substring')

Output :

Yes, List contains the substring

Yes, there is atleast a string in the list that contains the given substring.

Summary

We learned about different ways to check if a list of strings contain a substring or not.

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. Required fields are marked *

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

Scroll to Top