Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Remove strings with any non-required character

Improve Article
Save Article
  • Last Updated : 26 Oct, 2022
Improve Article
Save Article

Given a Strings list, remove string, if it contains any unwanted character.

Input : test_list = [“gfg”, “is”, “best”, “for”, “geeks”], chr_list = [‘f’, ‘m’, ‘n’, ‘i’] 
Output : [‘best’, ‘geeks’] 
Explanation : Given Strings don’t contain f, m, n or i.

Input : test_list = [“gfg”, “is”, “best”, “for”, “geeks”], chr_list = [‘f’, ‘m’, ‘n’] 
Output : [‘best’, ‘geeks’, ‘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", "geeks"]
 
# 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))

Output

The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
Filtered Strings : ['best', 'geeks']

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", "geeks"]
 
# 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))

Output

The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
Filtered Strings : ['best', 'geeks']

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", "geeks"]
 
# 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))

Output

The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
Filtered Strings : ['best', 'geeks']

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!