Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Filter list of strings based on the substring list

Improve Article
Save Article
  • Last Updated : 09 Mar, 2023
Improve Article
Save Article

Given two lists of strings string and substr, write a Python program to filter out all the strings in string that contains string in substr

Examples:

Input : string = [‘city1’, ‘class5’, ‘room2’, ‘city2’]
substr = [‘class’, ‘city’]
Output : [‘city1’, ‘class5’, ‘city2’]

Input : string = [‘coordinates’, ‘xyCoord’, ‘123abc’]
substr = [‘abc’, ‘xy’]
Output : [‘xyCoord’, ‘123abc’]

Method #1: Using List comprehension. 
We can use list comprehension along with in operator to check if the string in ‘substr’ is contained in ‘string’ or not. 

Python3




# Python3 program to Filter list of
# strings based on another list
import re
 
def Filter(string, substr):
    return [str for str in string if
             any(sub in str for sub in substr)]
     
# Driver code
string = ['city1', 'class5', 'room2', 'city2']
substr = ['class', 'city']
print(Filter(string, substr))

Output:

['city1', 'class5', 'city2']

Time complexity: O(n * m), where n is the number of strings in the input list “string” and m is the number of substrings in the input list “substr”.
Auxiliary space: O(1), as the function uses only a few variables and doesn’t create any additional data structures.

Method #2: Python Regex 

Python3




# Python3 program to Filter list of
# strings based on another list
import re
 
def Filter(string, substr):
    return [str for str in string
    if re.match(r'[^\d]+|^', str).group(0) in substr]
     
# Driver code
string = ['city1', 'class5', 'room2', 'city2']
substr = ['class', 'city']
print(Filter(string, substr))

Output:

['city1', 'class5', 'city2']

Method #3 : Using find() method.

find() method searches for the string that is passed as argument in given string and returns the position or else returns -1.

Python3




# Python3 program to Filter list of
# strings based on another list
 
string = ['city1', 'class5', 'room2', 'city2']
substr = ['class', 'city']
x=[]
for i in substr:
    for j in string:
        if(j.find(i)!=-1 and j not in x):
            x.append(j)
print(x)

Output

['class5', 'city1', 'city2']

Method #4 : Using the filter function and a lambda function:
The filter function is a built-in Python function that takes in two arguments: a function and an iterable. It returns an iterator that returns the elements of the iterable for which the function returns True.

In this case, we are using a lambda function as the first argument to the filter function. The lambda function takes in a string x and returns True if any of the substrings in the substrings list appear in x, and False otherwise. The second argument to the filter function is the strings list, which is the iterable that we want to filter.

Therefore, the filter function returns an iterator that returns all the elements of the strings list for which the lambda function returns True. In this case, the lambda function returns True for the elements ‘city1’, ‘class5’, and ‘city2’, so the iterator returned by the filter function will contain those elements.

Python3




# Initialize the list of strings and the list of substrings
strings = ['city1', 'class5', 'room2', 'city2']
substrings = ['class', 'city']
 
# Use the filter function and a lambda function to filter the strings
filtered_strings = list(filter(lambda x: any(substring in x for substring in substrings), strings))
 
# Print the filtered strings
print(filtered_strings)
#This code is contributed by Edula Vinay Kumar Reddy

Output

['city1', 'class5', 'city2']

Time complexity: O(n^2), where n is the length of the strings list
Auxiliary Space: O(n), where n is the length of the filtered_strings list

Method #5: Using a for loop:

Python3




# Define a function to filter a list of strings based on another list of substrings
def Filter(string, substr):
    # Create an empty list to store the filtered strings
    filtered_list = []
     
    # Loop over each string in the input list
    for s in string:
        # Loop over each substring in the filter list
        for sub in substr:
            # Check if the substring is in the current string
            if sub in s:
                # If it is, add the string to the filtered list and break out of the inner loop
                filtered_list.append(s)
                break
                 
    # Return the final filtered list
    return filtered_list
 
# Define the input list of strings and the filter list of substrings
string = ['city1', 'class5', 'room2', 'city2']
substr = ['class', 'city']
 
# Call the filter function with the input lists and print the result
filtered_list = Filter(string, substr)
print(filtered_list)

Output

['city1', 'class5', 'city2']

Time complexity: O(nm), where n is the length of the input string list and m is the length of the filter substring list.
Auxiliary space:  O(k), where k is the length of the filtered list.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!