Python - Remove String from String List
Last Updated :
24 Mar, 2023
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))
OutputOriginal 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))
OutputOriginal 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.
Similar Reads
Python - Remove substring list from String Our task is to remove multiple substrings from a string in Python using various methods like string replace in a loop, regular expressions, list comprehensions, functools.reduce, and custom loops. For example, given the string "Hello world!" and substrings ["Hello", "ld"], we want to get " wor!" by
3 min read
Python - Remove suffix from string list To remove a suffix from a list of strings, we identify and exclude elements that end with the specified suffix. This involves checking each string in the list and ensuring it doesn't have the unwanted suffix at the end, resulting in a list with only the desired elements.Using list comprehensionUsing
3 min read
Python | Substring removal in String list While working with strings, one of the most used application is removing the part of string with another. Since string in itself is immutable, the knowledge of this utility in itself is quite useful. Here the removing of a substring in list of string is performed. Letâs discuss certain ways in which
5 min read
Python - Remove after substring in String Removing everything after a specific substring in a string involves locating the substring and then extracting only the part of the string that precedes it. For example we are given a string s="Hello, this is a sample string" we need to remove the part of string after a particular substring includin
3 min read
Python | Remove prefix strings from list Sometimes, while working with data, we can have a problem in which we need to filter the strings list in such a way that strings starting with a specific prefix are removed. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + remove() + startswith() The combinati
5 min read
Python - Remove leading 0 from Strings List Sometimes, while working with Python, we can have a problem in which we have data which we need to perform processing and then pass the data forward. One way to process is to remove a stray 0 that may get attached to a string while data transfer. Let's discuss certain ways in which this task can be
5 min read
Remove spaces from a string in Python Removing spaces from a string is a common task in Python that can be solved in multiple ways. For example, if we have a string like " g f g ", we might want the output to be "gfg" by removing all the spaces. Let's look at different methods to do so:Using replace() methodTo remove all spaces from a s
2 min read
Python | Removing strings from tuple Sometimes we can come across the issue in which we receive data in form of tuple and we just want the numbers from it and wish to erase all the strings from them. This has a useful utility in Web-Development and Machine Learning as well. Let's discuss certain ways in which this particular task can b
4 min read
Python | Remove Kth character from strings list Sometimes, while working with data, we can have a problem in which we need to remove a particular column, i.e the Kth character from string list. String are immutable, hence removal just means re creating a string without the Kth character. Let's discuss certain ways in which this task can be perfor
7 min read
Python | Remove the given substring from end of string Sometimes we need to manipulate our string to remove extra information from the string for better understanding and faster processing. Given a task in which the substring needs to be removed from the end of the string using Python. Â Â Remove the substring from the end of the string using Slicing In
3 min read