Given a Strings list, remove string, if it contains any unwanted character.
Input : test_list = [“gfg”, “is”, “best”, “for”, “neveropen”], chr_list = [‘f’, ‘m’, ‘n’, ‘i’]
Output : [‘best’, ‘neveropen’]
Explanation : Given Strings don’t contain f, m, n or i.Input : test_list = [“gfg”, “is”, “best”, “for”, “neveropen”], chr_list = [‘f’, ‘m’, ‘n’]
Output : [‘best’, ‘neveropen’, ‘is’]
Explanation : Given Strings don’t contain f, m or n.
Method #1 : Using list comprehension + any()
In this, we test all strings and use any() to iterate all the non-required characters list and check its presence in string, if found, that list is not included in result list.
Python3
# Python3 code to demonstrate working of # Remove strings with any non-required character # Using list comprehension + any() # initializing list test_list = [ "gfg" , "is" , "best" , "for" , "neveropen" ] # printing original list print ( "The original list is : " + str (test_list)) # non-required char list chr_list = [ 'f' , 'm' , 'n' , 'i' ] # checking for all strings # removing if contains even 1 character res = [sub for sub in test_list if not any (ele in sub for ele in chr_list)] # printing result print ( "Filtered Strings : " + str (res)) |
The original list is : ['gfg', 'is', 'best', 'for', 'neveropen'] Filtered Strings : ['best', 'neveropen']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using filter() + lambda + any()
In this, we perform task of filtering using filter() and lambda function. Rest any() is used to check for any character present in strings for its removal.
Python3
# Python3 code to demonstrate working of # Remove strings with any non-required character # Using filter() + lambda + any() # initializing list test_list = [ "gfg" , "is" , "best" , "for" , "neveropen" ] # printing original list print ( "The original list is : " + str (test_list)) # non-required char list chr_list = [ 'f' , 'm' , 'n' , 'i' ] # checking for all strings # filter and lambda used to do this task res = list ( filter ( lambda sub: not any ( ele in sub for ele in chr_list), test_list)) # printing result print ( "Filtered Strings : " + str (res)) |
The original list is : ['gfg', 'is', 'best', 'for', 'neveropen'] Filtered Strings : ['best', 'neveropen']
Time Complexity: O(n) where n is the number of elements in the list “test_list”. filter() and lambda function performs n number of operations.
Auxiliary Space: O(1), no extra space is required
Method #3 : Using replace() method
Python3
# Python3 code to demonstrate working of # Remove strings with any non-required character # initializing list test_list = [ "gfg" , "is" , "best" , "for" , "neveropen" ] # printing original list print ( "The original list is : " + str (test_list)) # non-required char list chr_list = [ 'f' , 'm' , 'n' , 'i' ] # checking for all strings x = [] for i in test_list: for j in chr_list: i = i.replace(j,"") x.append(i) res = [] for i in range ( 0 , len (test_list)): if (test_list[i] = = x[i]): res.append(test_list[i]) # printing result print ( "Filtered Strings : " + str (res)) |
The original list is : ['gfg', 'is', 'best', 'for', 'neveropen'] Filtered Strings : ['best', 'neveropen']
Method#4: Using Recursive method.
Algorithm:
- Define a function remove_strings that takes a list of strings lst and a list of non-required characters chr_list as input.
- Check if the input list lst is empty, if yes then return an empty list.
- Otherwise, get the first string in the input list lst.
- Recursively call the remove_strings function on the rest of the input list lst.
- Check if the first string first contains any non-required character from the chr_list.
- If yes, exclude the string first from the result and return the remaining modified list rest.
- If no, include the string first in the result and return the modified list rest with first added to the front.
Python3
# Python3 code to demonstrate working of # Remove strings with any non-required character # Using recursive method def remove_strings(lst, chr_list): if not lst: # base case for empty list return [] else : # get the first string in the list first = lst[ 0 ] # recursively remove strings from the rest of the list rest = remove_strings(lst[ 1 :], chr_list) # check if the first string contains any non-required character if any (ele in first for ele in chr_list): # if yes, don't include it in the result return rest else : # otherwise, include it in the result return [first] + rest # initializing list test_list = [ "gfg" , "is" , "best" , "for" , "neveropen" ] # printing original list print ( "The original list is : " + str (test_list)) # non-required char list chr_list = [ 'f' , 'm' , 'n' , 'i' ] # checking for all strings # removing if contains even 1 character res = remove_strings(test_list,chr_list) # printing result print ( "Filtered Strings : " + str (res)) |
The original list is : ['gfg', 'is', 'best', 'for', 'neveropen'] Filtered Strings : ['best', 'neveropen']
The time complexity of this algorithm is O(nmk), where n is the length of the input list lst, m is the average length of the strings in lst, and k is the length of the chr_list. This is because we are looping through the input list lst once, and for each string in lst, we are checking for the presence of any non-required character from chr_list, which takes O(mk) time.
The auxiliary space of this algorithm is also O(nm), because we are creating a new list of modified strings of the same length as the input list. However, this algorithm has a recursive structure, so it may use additional space on the call stack.
METHOD 5:using map() and lambda function
APPROACH:
This approach uses the filter() and map() functions along with a lambda function to remove strings that contain any non-required character from a given list of strings.
ALGORITHM:
1.Initialize test_list and chr_list with given values.
2.Use map() function along with a lambda function to convert each string in test_list to an empty string if it contains any character from chr_list, otherwise keep the string as it is.
3.Use filter() function along with another lambda function to remove all empty strings from the list generated in step 2.
4.Store the filtered list in the result variable and print it.
Python3
test_list = [ "gfg" , "is" , "best" , "for" , "neveropen" ] chr_list = [ 'f' , 'm' , 'n' , 'i' ] result = list ( filter ( lambda x: x ! = " ", map(lambda x: x if all(c not in chr_list for c in x) else " ", test_list))) print (result) |
['best', 'neveropen']
Time complexity: O(n * k) where n is the length of the given list and k is the length of chr_list. This is because we need to check each character of each string in the list against every character in chr_list.
Space complexity: O(n) since we are storing the filtered list in memory. The space required for the test_list and chr_list is negligible.