Python – Remove suffix from string 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 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 listtest_list = ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']# printing original listprint("The original list : " + str(test_list))# initialize suffixsuff = 'x'# Suffix removal from String list# using loop + remove() + endswith()for word in test_list[:]: if word.endswith(suff): test_list.remove(word)# printing resultprint("List after removal of suffix elements : "+ str(test_list)) |
The original list : ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx'] List after removal of suffix elements : ['gfg', 'xit', 'is']
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 listtest_list = ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']# printing original listprint("The original list : " + str(test_list))# initialize suffsuff = 'x'# Suffix removal from String list# using list comprehension + endswith()res = [ele for ele in test_list if not ele.endswith(suff)]# printing resultprint("List after removal of suffix elements : "+ str(res)) |
The original list : ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx'] List after removal of suffix elements : ['gfg', 'xit', 'is']
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 suffsuff = 'x'def eva(x): return not x.endswith(suff)# initialize listtest_list = ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']# printing original listprint("The original list : " + str(test_list))# Remove prefix strings from list# using filter + endswith()res = list(filter(eva, test_list))# printing resultprint("List after removal of Kth character of each string : " + str(res)) |
The original list : ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx'] List after removal of Kth character of each string : ['gfg', 'xit', 'is']
Method #4 : Without any built-in methods
Python3
# Python3 code to demonstrate working of# Suffix removal from String list# initialize listtest_list = ["allx", "lovex", "gfg", "xit", "is", "bestx"]# printing original listprint("The original list : " + str(test_list))# initialize suffixsuff = "x"res = []# Suffix removal from String listfor i in test_list: if(i[-1] != suff): res.append(i)# printing resultprint("List after removal of suffix elements : " + str(res)) |
The original list : ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx'] List after removal of suffix elements : ['gfg', 'xit', 'is']


