Open In App

Python – Remove words containing list characters

Last Updated : 26 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# Remove words containing list characters
# using list comprehension + all()
from itertools import groupby
 
# initializing list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# initializing char list
char_list = ['g', 'o']
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing character list
print ("The character list is : " + str(char_list))
 
# Remove words containing list characters
# using list comprehension + all()
res = [ele for ele in test_list if all(ch not in ele for ch in char_list)]
 
# printing result
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




# Python3 code to demonstrate
# Remove words containing list characters
# using loop
from itertools import groupby
 
# initializing list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# initializing char list
char_list = ['g', 'o']
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing character list
print ("The character list is : " + str(char_list))
 
# Remove words containing list characters
# using loop
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)
 
# printing result
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




# Python3 code to demonstrate
# Remove words containing list characters
# initializing list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# initializing char list
char_list = ['g', 'o']
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing character list
print ("The character list is : " + str(char_list))
 
# Remove words containing list characters
 
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)
# printing result
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




# Python3 code to demonstrate
# Remove words containing list characters
from collections import Counter
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# initializing char list
char_list = ['g', 'o']
 
# printing original list
print("The original list is : " + str(test_list))
 
# printing character list
print("The character list is : " + str(char_list))
 
# Remove words containing list characters
 
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)
       
       
# printing result
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




# Python3 code to demonstrate
# Remove words containing list characters
from itertools import groupby
import operator as op
# initializing list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# initializing char list
char_list = ['g', 'o']
 
# printing original list
print("The original list is : " + str(test_list))
 
# printing character list
print("The character list is : " + str(char_list))
 
# Remove words containing list characters
# using loop
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)
 
# printing result
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




# Python3 code to demonstrate
# Remove words containing list characters
 
def remove_words(start,lst,charlst,newlist=[]):
  if start==len(lst):
    return newlist
  flag=0
  for i in charlst:
    if in lst[start]:
      flag=1
  if flag==0:
     newlist.append(lst[start])
  return remove_words(start+1,lst,charlst,newlist)
# initializing list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# initializing char list
char_list = ['g', 'o']
 
# printing original list
print("The original list is : " + str(test_list))
 
# printing character list
print("The character list is : " + str(char_list))
 
# Remove words containing list characters
# using recursive function
res = remove_words(0,test_list,char_list)
 
# printing result
print("The filtered strings are : " + str(res))
#this code contributed by tvsk


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']
# printing original list
print("The original list is : " + str(test_list))
  
# printing character 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




# Python3 code to demonstrate
# Remove words containing list characters
 
# initializing list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# initializing char list
char_list = set(['g', 'o'])
 
# printing original list
print("The original list is : " + str(test_list))
 
# printing character list
print("The character list is : " + str(char_list))
 
# Remove words containing list characters
res = list(filter(lambda x: not set(x).intersection(char_list), test_list))
 
# printing result
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.



Similar Reads

Python | Remove tuple from list of tuples if not containing any character
Given a list of tuples, the task is to remove all those tuples which do not contain any character value. Example: Input: [(', ', 12), ('...', 55), ('-Geek', 115), ('Geeksfor', 115),] Output: [('-Geek', 115), ('Geeksfor', 115)] Method #1 : Using list comprehension C/C++ Code # Python code to remove all those # elements from list of tuple # which doe
4 min read
Python | Remove element from given list containing specific digits
Given a list, the task is to remove all those elements from list which contains the specific digits. Examples: Input: lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16] no_delete = ['2', '3', '4', '0'] Output: [1, 5, 6, 7, 8, 9, 11, 15, 16] Explanation: Numbers 2, 3, 4, 10, 12, 13, 14 contains digits from no_delete, therefore remove them
9 min read
Python | Remove List elements containing given String character
Sometimes, while working with Python lists, we can have problem in which we need to perform the task of removing all the elements of list which contain at least one character of String. This can have application in day-day programming. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop This is brute force way in w
7 min read
How to Remove repetitive characters from words of the given Pandas DataFrame using Regex?
Prerequisite: Regular Expression in Python In this article, we will see how to remove continuously repeating characters from the words of the given column of the given Pandas Dataframe using Regex. Here, we are actually looking for continuously occurring repetitively coming characters for that we have created a pattern that contains this regular ex
2 min read
Python - Compute the frequency of words after removing stop words and stemming
In this article we are going to tokenize sentence, paragraph, and webpage contents using the NLTK toolkit in the python environment then we will remove stop words and apply stemming on the contents of sentences, paragraphs, and webpage. Finally, we will Compute the frequency of words after removing stop words and stemming. Modules Needed bs4: Beaut
8 min read
Python - Remove duplicate words from Strings in List
Sometimes, while working with Python list we can have a problem in which we need to perform removal of duplicated words from string list. This can have application when we are in data domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using set() + split() + loop The combination of above methods can be used to perfo
6 min read
Python | Convert list of strings and characters to list of characters
Sometimes we come forward to the problem in which we receive a list that consists of strings and characters mixed and the task we need to perform is converting that mixed list to a list consisting entirely of characters. Let's discuss certain ways in which this is achieved. Method #1 : Using List comprehension In this method, we just consider each
6 min read
Python | Get the smallest window in a string containing all characters of given pattern
Given two strings str and pattern, find the smallest substring in str containing all characters of pattern efficiently. Examples: Input : str = 'geeksforgeeks' pattern = 'gks' Output : geeks Input : str = 'new string' pattern = 'rg' Output : ring Approach #1 : Using Python enumerate() This method uses Python enumerate(). need[k] store how many time
5 min read
Python - Create nested list containing values as the count of list items
Given a list, the task is to write a Python program to create a nested list where the values are the count of list items. Examples: Input: [1, 2, 3] Output: [[1], [2, 2], [3, 3, 3]] Input: [4, 5] Output: [[1, 1, 1, 1], [2, 2, 2, 2, 2]] Method 1: Using nested list comprehension The list will contain the count of the list items for each element e in
2 min read
Python | Remove all duplicates words from a given sentence
Given a sentence containing n words/strings. Remove all duplicates words/strings which are similar to each others. Examples: Input : Geeks for Geeks Output : Geeks for Input : Python is great and Java is also great Output : is also Java Python and great We can solve this problem quickly using python Counter() method. Approach is very simple. 1) Spl
7 min read
Practice Tags :
three90RightbarBannerImg