Python | Remove prefix strings from list
Last Updated :
11 Apr, 2023
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 combination of the above functions can solve this problem. In this, we remove the elements that start with a particular prefix accessed using loop and return the modified list.
Python3
# Python3 code to demonstrate working of
# Remove prefix strings from list
# using loop + remove() + startswith()
# initialize list
test_list = ['xall', 'xlove', 'gfg', 'xit', 'is', 'best']
# printing original list
print("The original list : " + str(test_list))
# initialize prefix
pref = 'x'
# Remove prefix strings from list
# using loop + remove() + startswith()
for word in test_list[:]:
if word.startswith(pref):
test_list.remove(word)
# printing result
print("List after removal of Kth character of each string : " + str(test_list))
Output : The original list : ['xall', 'xlove', 'gfg', 'xit', 'is', 'best']
List after removal of Kth character of each string : ['gfg', 'is', 'best']
Time complexity: O(n^2)
Auxiliary space: O(1)
Method #2: Using list comprehension + startswith()
This is another way in which this task can be performed. In this, we don't perform removal in place, instead, we recreate the list without the elements that match the prefix.
Python3
# Python3 code to demonstrate working of
# Remove prefix strings from list
# using list comprehension + startswith()
# initialize list
test_list = ['xall', 'xlove', 'gfg', 'xit', 'is', 'best']
# printing original list
print("The original list : " + str(test_list))
# initialize prefix
pref = 'x'
# Remove prefix strings from list
# using list comprehension + startswith()
res = [ele for ele in test_list if not ele.startswith(pref)]
# printing result
print("List after removal of Kth character of each string : " + str(res))
Output : The original list : ['xall', 'xlove', 'gfg', 'xit', 'is', 'best']
List after removal of Kth character of each string : ['gfg', 'is', 'best']
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(m), where m is the length of the output list res.
Method#3: Using filter() + startswith()
This is another way in which this task can be performed. In this, we create new list by using filter function from the old list. Filter function filter out string which starts with defined prefix.
Python3
# Python3 code to demonstrate working of
# Remove prefix strings from list
# using filter + startswith()
# initialize prefix
pref = 'x'
def eva(x):
return not x.startswith(pref)
# initialize list
test_list = ['xall', 'xlove', 'gfg', 'xit', 'is', 'best']
# printing original list
print("The original list : " + str(test_list))
# Remove prefix strings from list
# using filter + startswith()
res = list(filter(eva, test_list))
# printing result
print("List after removal of Kth character of each string : " + str(res))
OutputThe original list : ['xall', 'xlove', 'gfg', 'xit', 'is', 'best']
List after removal of Kth character of each string : ['gfg', 'is', 'best']
Method #4: Without using startswith() method
Python3
# Python3 code to demonstrate working of
# Prefix removal from String list
# initialize list
test_list = ['xall', 'xlove', 'gfg', 'xit', 'is', 'best']
# printing original list
print("The original list : " + str(test_list))
# initialize prefix
pref = "x"
res=[]
# Prefixx removal from String list
for i in test_list:
if(i[0]!=pref):
res.append(i)
# printing result
print("List after removal of prefix elements : " + str(res))
OutputThe original list : ['xall', 'xlove', 'gfg', 'xit', 'is', 'best']
List after removal of suffix elements : ['gfg', 'is', 'best']
Method #5: Using find() method
Python3
# Python3 code to demonstrate working of
# Prefix removal from String list
# initialize list
test_list = ['xall', 'xlove', 'gfg', 'xit', 'is', 'best']
# printing original list
print("The original list : " + str(test_list))
# initialize suffix
pref = "x"
res=[]
# Suffix removal from String list
for i in test_list:
if(i.find(pref)!=0):
res.append(i)
# printing result
print("List after removal of suffix elements : " + str(res))
OutputThe original list : ['xall', 'xlove', 'gfg', 'xit', 'is', 'best']
List after removal of suffix elements : ['gfg', 'is', 'best']
The time complexity of this program is O(n*m), where n is the length of the list and m is the length of the prefix string being searched for.
The auxiliary space complexity of this program is O(k), where k is the number of strings in the list that do not start with the prefix string.
Method #5 : Using re.match()
This method makes use of the re (regular expression) module in python. The match function returns a match object if the pattern defined in the regular expression is found at the start of the string.
Steps:
- Initialize a list of strings named test_list containing a few strings.
- Print the original list using the print() function and str() function to convert the list to a string.
- Initialize a string variable named pref that will be used to check for the prefix.
- Use a list comprehension to iterate over each word in the test_list and remove the words that have the specified prefix using the re.match() function.
- Store the resulting list in the res variable.
- Print the resulting list using the print() function and str() function to convert the list to a string.
Python3
import re
# initialize list
test_list = ['xall', 'xlove', 'gfg', 'xit', 'is', 'best']
# printing original list
print("The original list : " + str(test_list))
# initialize prefix
pref = 'x'
# Remove prefix strings from list
# using re.match()
res = [word for word in test_list if not re.match(pref, word)]
# printing result
print("List after removal of prefix strings : " + str(res))
#this code is contributed by edula vinay kumar reddy
OutputThe original list : ['xall', 'xlove', 'gfg', 'xit', 'is', 'best']
List after removal of prefix strings : ['gfg', 'is', 'best']
Time Complexity: O(n)
Auxiliary space: O(n)
Similar Reads
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 - Remove String from String List
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 certa
4 min read
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 all digits from a list of strings
The problem is about removing all numeric digits from each string in a given list of strings. We are provided with a list where each element is a string and the task is to remove any digits (0-9) from each string, leaving only the non-digit characters. In this article, we'll explore multiple methods
4 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
Python | Remove all strings from a list of tuples
Given a list of tuples, containing both integer and strings, the task is to remove all strings from list of tuples. Examples: Input : [(1, 'Paras'), (2, 'Jain'), (3, 'GFG'), (4, 'Cyware')] Output : [(1), (2), (3), (4)] Input : [('string', 'Geeks'), (2, 225), (3, '111')] Output : [(), (2, 225), (3,)]
8 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 Punctuation from String
In this article, we will explore various methods to Remove Punctuations from a string.Using str.translate() with str.maketrans()str.translate() method combined with is str.maketrans() one of the fastest ways to remove punctuation from a string because it works directly with string translation tables
2 min read
Prefix frequency in string List - Python
In this article, we will explore various methods to find prefix frequency in string List. The simplest way to do is by using a loop.Using a LoopOne of the simplest ways to calculate the frequency of a prefix in a list of strings is by iterating through each element and checking if the string starts
2 min read
Remove Duplicate Strings from a List in Python
Removing duplicates helps in reducing redundancy and improving data consistency. In this article, we will explore various ways to do this. set() method converts the list into a set, which automatically removes duplicates because sets do not allow duplicate values.Pythona = ["Learn", "Python", "With"
3 min read