Python – Remove words containing list characters
Last Updated :
26 Mar, 2023
Sometimes, in the process of data filtering we have a problem in which we need to remove words which are composite of certain letters. This kind of application is common in data science domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using all() + list comprehension The combination of above methods can be used to perform this task. In this, we just check for all list characters using all() in each list and filters out string which has any one of characters.
Python3
from itertools import groupby
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
char_list = [ 'g' , 'o' ]
print ( "The original list is : " + str (test_list))
print ( "The character list is : " + str (char_list))
res = [ele for ele in test_list if all (ch not in ele for ch in char_list)]
print ( "The filtered strings are : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
The character list is : ['g', 'o']
The filtered strings are : ['is', 'best']
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the new res list
Method #2 : Using loop This is brute method in which this task can be performed. In this we use loop and conditional statements to perform this task.
Python3
from itertools import groupby
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
char_list = [ 'g' , 'o' ]
print ( "The original list is : " + str (test_list))
print ( "The character list is : " + str (char_list))
res = []
flag = 1
for ele in test_list:
for idx in char_list:
if idx not in ele:
flag = 1
else :
flag = 0
break
if (flag = = 1 ):
res.append(ele)
print ( "The filtered strings are : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
The character list is : ['g', 'o']
The filtered strings are : ['is', 'best']
Time Complexity: O(n*n)
Auxiliary Space: O(n), where n is length of list.
Method #3 : Using replace() and len() methods
Python3
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
char_list = [ 'g' , 'o' ]
print ( "The original list is : " + str (test_list))
print ( "The character list is : " + str (char_list))
res = []
for i in test_list:
x = i
for j in char_list:
i = i.replace(j,"")
if ( len (i) = = len (x)):
res.append(i)
print ( "The filtered strings are : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
The character list is : ['g', 'o']
The filtered strings are : ['is', 'best']
Time Complexity : O(N*N)
Auxiliary space : O(1)
Method #4 : Using Counter() function
Python3
from collections import Counter
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
char_list = [ 'g' , 'o' ]
print ( "The original list is : " + str (test_list))
print ( "The character list is : " + str (char_list))
res = []
freqCharList = Counter(char_list)
for i in test_list:
unique = True
for char in i:
if char in freqCharList.keys():
unique = False
break
if (unique):
res.append(i)
print ( "The filtered strings are : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
The character list is : ['g', 'o']
The filtered strings are : ['is', 'best']
Time Complexity : O(N*N)
Auxiliary space : O(N)
Method #5: Using operator.countOf() method
Python3
from itertools import groupby
import operator as op
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
char_list = [ 'g' , 'o' ]
print ( "The original list is : " + str (test_list))
print ( "The character list is : " + str (char_list))
res = []
flag = 1
for ele in test_list:
for idx in char_list:
if op.countOf(ele, idx) = = 0 :
flag = 1
else :
flag = 0
break
if (flag = = 1 ):
res.append(ele)
print ( "The filtered strings are : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
The character list is : ['g', 'o']
The filtered strings are : ['is', 'best']
Time Complexity : O(N*N)
Auxiliary space : O(1)
Method #6: Using the recursive method.
Python3
def remove_words(start,lst,charlst,newlist = []):
if start = = len (lst):
return newlist
flag = 0
for i in charlst:
if i in lst[start]:
flag = 1
if flag = = 0 :
newlist.append(lst[start])
return remove_words(start + 1 ,lst,charlst,newlist)
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
char_list = [ 'g' , 'o' ]
print ( "The original list is : " + str (test_list))
print ( "The character list is : " + str (char_list))
res = remove_words( 0 ,test_list,char_list)
print ( "The filtered strings are : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
The character list is : ['g', 'o']
The filtered strings are : ['is', 'best']
Time Complexity : O(N*N)
Auxiliary space : O(N)
Method #7: Using filter() and lambda function:
Python3
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
char_list = [ 'g' , 'o' ]
print ( "The original list is : " + str (test_list))
print ( "The character list is : " + str (char_list))
res = list ( filter ( lambda x: not any (y in x for y in char_list), test_list))
print (res)
|
Output
The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
The character list is : ['g', 'o']
['is', 'best']
Time Complexity : O(N*N)
Auxiliary space : O(N)
Method #8 : Using set intersection
Python3
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
char_list = set ([ 'g' , 'o' ])
print ( "The original list is : " + str (test_list))
print ( "The character list is : " + str (char_list))
res = list ( filter ( lambda x: not set (x).intersection(char_list), test_list))
print ( "The filtered strings are : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
The character list is : {'o', 'g'}
The filtered strings are : ['is', 'best']
Time complexity: O(N), where N is the number of elements in test_list
Auxiliary Space: O(N), in the worst case, all elements in test_list are kept in the resulting list
Explanation: This method uses filter and set to achieve the desired result. set is used to convert the list of characters into a set, which allows for constant time look ups. filter is used to keep only those elements from test_list for which the intersection with char_list is empty.
Please Login to comment...