Python - Filter list of strings based on the substring list Last Updated : 15 Jan, 2025 Comments Improve Suggest changes Like Article Like Report The problem requires to check which strings in the main list contain any of the substrings from a given list and keep only those that match. Let us explore this problem and understand different methods to solve it.Using list comprehension with any() (Most Efficient)List comprehension is a concise and efficient way to solve problems like this. By combining it with the any() function, we can easily check if a string contains any substring from the list of substrings. Python s = ["learn", "python", "with", "gfg"] subs = ["le", "py"] # List of substrings to check for in the strings # List comprehension that filters strings from 's' if any of the substrings in 'subs' are found in them res = [x for x in s if any(y in x for y in subs)] print(res) Output['learn', 'python'] Explanation:We use a list comprehension to iterate over each string in the list 's'.Inside the comprehension, the any() function checks if any substring from subs is present in the current string.Only the strings that meet the condition are added to the result.Let’s explore some more different methods to filter list of strings based on the substring list.Table of ContentUsing nested loopsUsing filter with a lambda functionUsing regular expressionsUsing nested loopsThis method is more straightforward but less efficient. We use two loops: one for the list of strings and one for the list of substrings. Python s = ["learn", "python", "with", "gfg"] subs = ["le", "py"] # List of substrings to check for in the strings res = [] # List to store the result # Iterate through each string in 's' for x in s: # Iterate through each substring in 'subs' for y in subs: # If a substring 'y' is found in the string 'x' if y in x: res.append(x) # Add the string 'x' to the result list break # Exit the inner loop once a match is found print(res) Output['learn', 'python'] Explanation:We loop through each string in 's' and each substring in subs.If a substring is found in the string, we add it to the result and stop checking further substrings for that string.This method is easy to understand but can be slower for larger lists.Using filter with a lambda functionfilter() function provides a functional programming approach. We use a lambda function to define the condition. Python s = ["learn", "python", "with", "gfg"] subs = ["le", "py"] # List of substrings to check for in the strings # Use filter and lambda to filter strings in 's' if any of the substrings in 'subs' are found res = list(filter(lambda x: any(y in x for y in subs), s)) print(res) Output['learn', 'python'] Explanation:The filter() function applies the lambda function to each element in 's'.The lambda function uses any to check if a string contains any of the substrings from subs.The filter function returns an iterator, which we convert into a list.Using regular expressionsWe can use the re module to solve this problem. Regular expressions are a versatile tool for pattern matching and can be used to solve this problem effectively. Python import re s = ["learn", "python", "with", "gfg"] # subs = ["le", "py"] # List of substrings to check for in the strings # Join substrings in 'subs' with '|' (OR operator) to create a regular expression pattern pattern = "|".join(subs) # List comprehension that filters strings from 's' if the pattern matches using regular expressions res = [x for x in s if re.search(pattern, x)] print(res) Output['learn', 'python'] Explanation:We create a regular expression pattern by joining all substrings with the pipe operator.re.search() function checks if the pattern matches any part of the string.Only matching strings are added to the result. Comment More infoAdvertise with us Next Article Python - Filter list of strings based on the substring list S Smitha Dinesh Semwal Follow Improve Article Tags : Python Python Programs Python string-programs python Practice Tags : pythonpython Similar Reads Filter a list based on the Given List of Strings - Python The task of filtering a list based on given strings involves removing elements that contain specific keywords. Given a list of file paths and filter words, the goal is to exclude paths containing any specified words. For example, with filter words ['key', 'keys', 'keyword'] and file paths ['home/key 3 min read Create List of Substrings from List of Strings in Python In Python, when we work with lists of words or phrases, we often need to break them into smaller pieces, called substrings. A substring is a contiguous sequence of characters within a string. Creating a new list of substrings from a list of strings can be a common task in various applications. In th 3 min read Extract List of Substrings in List of Strings in Python Working with strings is a fundamental aspect of programming, and Python provides a plethora of methods to manipulate and extract substrings efficiently. When dealing with a list of strings, extracting specific substrings can be a common requirement. In this article, we will explore five simple and c 3 min read Python - All occurrences of Substring from the list of strings Given a list of strings and a list of substring. The task is to extract all the occurrences of a substring from the list of strings. Examples: Input : test_list = ["gfg is best", "gfg is good for CS", "gfg is recommended for CS"] subs_list = ["gfg", "CS"] Output : ['gfg is good for CS', 'gfg is reco 5 min read Python | Split list of strings into sublists based on length Given a list of strings, write a Python program to split the list into sublists based on string length. Examples: Input : ['The', 'art', 'of', 'programming'] Output : [['of'], ['The', 'art'], ['programming']] Input : ['Welcome', 'to', 'geeksforgeeks'] Output : [['to'], ['Welcome'], ['geeksforgeeks'] 3 min read Python | Filter String with substring at specific position Sometimes, while working with Python string lists, we can have a problem in which we need to extract only those lists that have a specific substring at a specific position. This kind of problem can come in data processing and web development domains. Let us discuss certain ways in which this task ca 5 min read Finding Strings with Given Substring in List - Python The task of finding strings with a given substring in a list in Python involves checking whether a specific substring exists within any of the strings in a list. The goal is to efficiently determine if the desired substring is present in any of the elements of the list. For example, given a list a = 3 min read Python - Filter Strings combination of K substrings Given a Strings list, extract all the strings that are a combination of K substrings. Input : test_list = ["geeks4u", "allbest", "abcdef"], substr_list = ["s4u", "est", "al", "ge", "ek", "def"], K = 3 Output : ['geeks4u'] Explanation : geeks4u made up of 3 substr - ge, ek and s4u. Input : test_list 4 min read Python | Get the string after occurrence of given substring The problem involves getting the string that is occurring after the substring has been found. Let's discuss certain ways in which this task can be performed using Python.Using partition()To extract the portion of a string that occurs after a specific substring partition() method is an efficient and 3 min read Count Occurance of Substring in a List of Strings - Python To count the occurrences of a particular substring in a list of strings in Python, we can use several methods. In this article, we are going to explore different methods to count the existence of a particular substring in a given list.Using sum() and Generator ExpressionThis method uses a generator 2 min read Like