The Wayback Machine - https://web.archive.org/web/20230208143253/https://www.geeksforgeeks.org/python-remove-suffix-from-string-list/
Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Remove suffix from string list

Improve Article
Save Article
  • Last Updated : 07 Feb, 2023
Improve Article
Save Article

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 ending with specific suffixes are removed. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using loop + remove() + endswith() Method

The combination of the above functions can solve this problem. In this, we remove the elements that end with a particular suffix accessed using a loop and return the modified list. 

Python3




# Python3 code to demonstrate working of
# Suffix removal from String list
# using loop + remove() + endswith()
 
# initialize list
test_list = ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize suffix
suff = 'x'
 
# Suffix removal from String list
# using loop + remove() + endswith()
for word in test_list[:]:
    if word.endswith(suff):
        test_list.remove(word)
 
# printing result
print("List after removal of suffix elements : "+ str(test_list))

Output

The original list : ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
List after removal of suffix elements : ['gfg', 'xit', 'is']

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #2: Using list comprehension + endswith() Method

This is another way in which this task can be performed. In this, we don’t perform the removal in place, instead, we recreate the list without the elements that match the suffix. 

Python3




# Python3 code to demonstrate working of
# Suffix removal from String list
# using list comprehension + endswith()
 
# initialize list
test_list = ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize suff
suff = 'x'
 
# Suffix removal from String list
# using list comprehension + endswith()
res = [ele for ele in test_list if not ele.endswith(suff)]
 
# printing result
print("List after removal of suffix elements : "+ str(res))

Output

The original list : ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
List after removal of suffix elements : ['gfg', 'xit', 'is']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method#3: Using filter function + endswith() Method

This is another way in which a task can be performed. In this, we can create a new list with the help of a filter function which filters out all the string which ends with a defined suffix.

Python3




# Python3 code to demonstrate working of
# Remove prefix strings from list
# using filter + endswith()
 
# initialize suff
suff = 'x'
 
def eva(x):
    return not x.endswith(suff)
# initialize list
test_list = ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
 
# printing original list
print("The original list : " + str(test_list))
 
# Remove prefix strings from list
# using filter + endswith()
res = list(filter(eva, test_list))
 
# printing result
print("List after removal of Kth character of each string : " + str(res))

Output

The original list : ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
List after removal of Kth character of each string : ['gfg', 'xit', 'is']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4 :  Without any built-in methods

Python3




# Python3 code to demonstrate working of
# Suffix removal from String list
 
# initialize list
test_list = ["allx", "lovex", "gfg", "xit", "is", "bestx"]
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize suffix
suff = "x"
 
res = []
# Suffix removal from String list
for i in test_list:
    if(i[-1] != suff):
        res.append(i)
 
# printing result
print("List after removal of suffix elements : " + str(res))

Output

The original list : ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
List after removal of suffix elements : ['gfg', 'xit', 'is']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #5 : Using find() and len() methods

Python3




# Python3 code to demonstrate working of
# Suffix removal from String list
 
# initialize list
test_list = ["allx", "lovex", "gfg", "xit", "is", "bestx"]
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize suffix
suff = "x"
 
res = []
# Suffix removal from String list
for i in test_list:
    if(i.find(suff)!=len(i)-1):
        res.append(i)
 
# printing result
print("List after removal of suffix elements : " + str(res))

Output

The original list : ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
List after removal of suffix elements : ['gfg', 'xit', 'is']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #6 : Using lambda functions

Python3




# Python3 code to demonstrate working of
# Remove prefix strings from list
 
# initialize suff
suff = 'x'
 
# initialize list
test_list = ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
 
# printing original list
print("The original list : " + str(test_list))
 
# Remove prefix strings from list
 
res = list(filter(lambda x: x[-1] != suff, test_list))
 
# printing result
print("List after removal of Kth character of each string : " + str(res))

Output

The original list : ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
List after removal of Kth character of each string : ['gfg', 'xit', 'is']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #7 : Using Regular Expressions
This method uses the regular expression module ‘re’ to match the strings in the list that end with a specific suffix and remove them. Here is an example of how it can be done:

Python3




import re
 
# initialize list
test_list = ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize suffix
suff = 'x$'
 
# Suffix removal from String list using regular expressions
res = [x for x in test_list if not re.search(suff, x)]
 
# printing result
print("List after removal of suffix elements : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy

Output

The original list : ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
List after removal of suffix elements : ['gfg', 'xit', 'is']

Time Complexity: O(n)
Auxiliary Space: O(n)

Here we use the function re.search() which returns a match object if there is a match anywhere in the string. We use this function in a list comprehension to filter out the elements in the list that match the regular expression.

Method#8: Using pandas

Python3




import pandas as pd
 
test_list = ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
# printing original list
 
print("The original list : " + str(test_list))
suffix = 'x'
 
# Convert the list of strings to a pandas series
 
s = pd.Series(test_list)
 
# Remove elements with the suffix
 
res = s[~s.str.endswith(suffix)].tolist()
 
print("List after removal of suffix elements :", res)
 
#This code is contributed by Vinay Pinjala.

Output

The original list : ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
List after removal of suffix elements : ['gfg', 'xit', 'is']

Time Complexity: O(n)
Auxiliary Space: O(n)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!