Find Substring within List of Strings in Python (2 Examples)

 

Hi! This short tutorial will show you how to detect a substring within a list of strings in the Python programming language.

Here is an overview:

Let’s dive into the Python code!

 

Create Demo List

Here, we will create the demo Python list of strings that we will use in the examples in this tutorial.

Therefore, in your Python programming IDE, run the code below to create the demo list:

my_list = ["apple", "banana", "orange", "grape"]
 
print(my_list)
 
# ['apple', 'banana', 'orange', 'grape']
 
print(type(my_list))
 
# <class 'list'>

Now that we have created the demo list, we will examine two ways to get a substring within the list. Below is the substring we want to find in the list:

substring = "ap"

 

Example 1: Detect Substring within List Using filter() Function

In this example, we will use the filter() function to get the substring in the list of strings:

strings = list(filter(lambda string: substring in string, my_list))
 
print(strings)
 
# ['apple', 'grape']
 
print(type(strings))
 
# <class 'list'>

Here, the filter() function takes a function and an iterable as input and returns an iterator that contains elements from the iterable for which the function returns True.

In this case, we use the filter() function along with a lambda function to check if the substring exists in each string within my_list.

The lambda function lambda string : substring in string returns True if the substring is found in the string.

The filter() function applies this lambda function to each string in my_list and returns an iterator with only the strings that satisfy the condition.

Finally, we convert the iterator to a list using list() function and store the result in strings.
 

Example 2: Detect Substring within List Using for Loop

In this second example, we will use a for loop to detect the substring in the list of strings:

strings = []
 
for string in my_list:
  if substring in string:
    strings.append(string)
 
print(strings)
 
# ['apple', 'grape']
 
print(type(strings))
 
# <class 'list'>

In the example above, the for loop iterates over each string in my_list. For each string, it checks if the substring exists in that string using the condition if substring in string.

If the condition is true, indicating that the substring is present in the string, the string is appended to the strings.

After iterating through all the strings in my_list, strings will contain only the strings that contain the desired substring.

This solution manually checks each string one by one and builds the strings list by appending the matching strings, providing a straightforward approach to find substrings within a list of strings.
 

Video, Further Resources & Summary

Do you need more explanations on how to find a substring within a list of strings in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to find a substring within a list of strings in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown, using two examples, how to find a substring within a list of strings in Python. Your use case will determine which method to adopt.

I hope you found this tutorial helpful! In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top