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
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'>
In this case, we use the
filter() function along with a lambda function to check if the substring exists in each string within
The lambda function
True if the substring is found in the string.
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
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 the condition is true, indicating that the substring is present in the string, the string is appended to the
After iterating through all the strings in
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:
- Find Index in List of Lists in Python (3 Examples)
- Lists in Python (13 Examples)
- Are Dictionaries Faster than Lists in Python?
- What is List Node in Python? (2 Examples)
- count() Method for Lists in Python (2 Examples)
- Introduction to Python Programming
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.
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.
Statistics Globe Newsletter