The Wayback Machine - https://web.archive.org/web/20240127162256/https://www.geeksforgeeks.org/python-remove-string-from-string-list/
Open In App
Related Articles

Python – Remove String from String List

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

This particular article is indeed a very useful one for Machine Learning enthusiast as it solves a good problem for them. In Machine Learning we generally encounter this issue of getting a particular string in huge amount of data and handling that sometimes becomes a tedious task. Lets discuss certain way outs to solve this problem. 

Method #1: Using remove() This particular method is quite naive and not recommended to use, but is indeed a method to perform this task. remove() generally removes the first occurrence of K string and we keep iterating this process until no K string is found in list. 

Python3




# Python 3 code to demonstrate
# Remove K String from String List
# using remove()
 
# initializing list
test_list = ["bad", "GeeksforGeeks", "bad", "is", "best", "bad"]
 
# Printing original list
print("Original list is : " + str(test_list))
 
# initializing K
K = "bad"
 
# using remove() to
# Remove K String from String List
while(K in test_list):
    test_list.remove(K)
 
# Printing modified list
print("Modified list is : " + str(test_list))


Output : 

Original list is : ['bad', 'GeeksforGeeks', 'bad', 'is', 'best', 'bad']
Modified list is : ['GeeksforGeeks', 'is', 'best']

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant extra space is required

Method #2: Using List Comprehension More concise and better approach to remove all the K strings, it just checks if the string is not K and re-makes the list with all strings that are not K. 

Python3




# Python 3 code to demonstrate
# Remove K String from String List
# using list comprehension
 
# initializing list
test_list = ["bad", "GeeksforGeeks", "bad", "is", "best", "bad"]
 
# Printing original list
print("Original list is : " + str(test_list))
 
# initializing K
K = "bad"
 
# using list comprehension to
# Remove K String from String List
test_list = [i for i in test_list if i != K]
 
# Printing modified list
print("Modified list is : " + str(test_list))


Output : 

Original list is : ['bad', 'GeeksforGeeks', 'bad', 'is', 'best', 'bad']
Modified list is : ['GeeksforGeeks', 'is', 'best']

Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.

Method #3 : Using join(),replace(),split() and remove() methods

Python3




# Python 3 code to demonstrate
# Remove K String from String List
 
# initializing list
test_list = ["bad", "GeeksforGeeks", "bad", "is", "best", "bad"]
 
# Printing original list
print ("Original list is : " + str(test_list))
 
# initializing K
K = "bad"
 
x="-".join(test_list)
x=x.replace(K,"")
a=x.split("-")
while("" in a ):
    a.remove("")
# Printing modified list
print ("Modified list is : " + str(a))


Output

Original list is : ['bad', 'GeeksforGeeks', 'bad', 'is', 'best', 'bad']
Modified list is : ['GeeksforGeeks', 'is', 'best']

Here’s another approach to removing a string ‘K’ from a string list, using a filter:

Python




# Python 3 code to demonstrate
# Remove K String from String List
# using filter()
  
# initializing list
test_list = ["bad", "GeeksforGeeks", "bad", "is", "best", "bad"]
  
# Printing original list
print("Original list is : " + str(test_list))
  
# initializing K
K = "bad"
  
# using filter to Remove K String from String List
test_list = list(filter(lambda x: x!=K, test_list))
  
# Printing modified list
print("Modified list is : " + str(test_list))


Output

Original list is : ['bad', 'GeeksforGeeks', 'bad', 'is', 'best', 'bad']
Modified list is : ['GeeksforGeeks', 'is', 'best']

This approach uses the filter() function to create a filtered list of elements from test_list that are not equal to K. The list() function is then used to convert the filtered list into a list.

Time complexity: O(n), where n is the number of elements in the list test_list.

Space complexity: O(n), since a new list is created.


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 24 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials