Open In App

Python | Remove prefix strings from list

Last Updated : 11 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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))


Output

The 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))


Output

The 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))


Output

The 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:

  1. Initialize a list of strings named test_list containing a few strings.
  2. Print the original list using the print() function and str() function to convert the list to a string.
  3. Initialize a string variable named pref that will be used to check for the prefix.
  4. 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.
  5. Store the resulting list in the res variable.
  6. 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


Output

The 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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads