Python - Remove suffix from string list
Last Updated :
14 Jan, 2025
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 comprehension
Using List Comprehension, we can create a new list that excludes the elements with the unwanted suffix, avoiding the overhead of modifying the list in place.
Python
li= ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
# Suffix to remove
s= 'x'
res = [word for word in li if not word.endswith(s)]
print(res)
Output['gfg', 'xit', 'is']
Explanation:
for word in li
: Thisloops through each element in the list li
.if not word.endswith(suff):
This
checks whether each word
does not end with the suffix s
.
Using filter()
filter() function in Python can be used to filter out elements from a list based on a condition. In this case, it is combined with the endswith() method to exclude elements that end with a specified suffix.
Python
li= ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
# Suffix to remove
s = 'x'
res = list(filter(lambda word: not word.endswith(s), li))
print(res)
Output['gfg', 'xit', 'is']
Explanation:
filter():
This
filters elements based on a condition.lambda word: not word.endswith(s):
This
checks if a word does not end with the suffix 'x'
.not word.endswith(s):
This
excludes words that end with 'x'
.list()
: This converts the result into a list.
Using remove()
This method involves iterating through the list and removing elements in place. However, modifying the list while iterating over it can cause issues with skipped elements, so a copy of the list or iterating over a slice is often required.
Python
li = ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
# Suffix
s = 'x'
for word in li[:]:
if word.endswith(s):
li.remove(word)
print(li)
Output['gfg', 'xit', 'is']
Explanation:
- for word in li[:] :This iterates over a copy of the list, ensuring changes don't affect the loop.
- if word.endswith(s): This checks if the word ends with the suffix 'x'.
- li.remove(word): This removes the word from the original list if it matches the condition, only removing the first occurrence.
Using pop()
This method iterates through the list and removes elements in place using pop()
. Modifying the list while iterating can cause elements to shift, potentially leading to skipped checks. Proper index handling is required to ensure all elements are checked.
Python
li = ['allx', 'lovex', 'gfg', 'xit', 'is', 'bestx']
# Suffix to remove
suff = 'x'
index = 0 # Start at the beginning of the list
while index < len(li): # Loop through the list
if li[index].endswith(suff):
li.pop(index)
else:
index += 1
print(li)
Output['gfg', 'xit', 'is']
Explanation:
- if li[index].endswith(suff): This checks if the current word ends with the suffix 'x'.
- li.pop(index): This removes the word at the current index if the condition is met.
- else: index += 1: This increments the index to check the next word if the condition is not met.
Similar Reads
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 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
Replace Substrings from String List - Python The task of replacing substrings in a list of strings involves iterating through each string and substituting specific words with their corresponding replacements. For example, given a list a = ['GeeksforGeeks', 'And', 'Computer Science'] and replacements b = [['Geeks', 'Gks'], ['And', '&'], ['C
3 min read