Python – Remove Rear K characters from String List
Last Updated :
06 Apr, 2023
Sometimes, we come across an issue in which we require to delete the last characters from each string, that we might have added by mistake and we need to extend this to the whole list. This type of utility is common in web development. Having shorthands to perform this particular job is always a plus. Let’s discuss certain ways in which this can be achieved.
Method #1: Using list comprehension + list slicing This task can be performed by using the ability of list slicing to remove the characters and the list comprehension helps in extending that logic to the whole list.
Python3
test_list = [ 'Manjeets' , 'Akashs' , 'Akshats' , 'Nikhils' ]
print ( "The original list : " + str (test_list))
K = 4
res = [sub[: len (sub) - K] for sub in test_list]
print ( "The list after removing last characters : " + str (res))
|
Output :
The original list : ['Manjeets', 'Akashs', 'Akshats', 'Nikhils']
The list after removing last characters : ['Manj', 'Ak', 'Aks', 'Nik']
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #2: Using map() + lambda The map function can perform the task of getting the functionality executed for all the members of list and lambda function performs the task of removal of last elements using list comprehension.
Python3
test_list = [ 'Manjeets' , 'Akashs' , 'Akshats' , 'Nikhils' ]
print ( "The original list : " + str (test_list))
K = 4
res = list ( map ( lambda i: i[: ( len (i) - K)], test_list))
print ( "The list after removing last characters : " + str (res))
|
Output :
The original list : ['Manjeets', 'Akashs', 'Akshats', 'Nikhils']
The list after removing last characters : ['Manj', 'Ak', 'Aks', 'Nik']
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #3: Using negative indexing and slicing
Python3
test_list = [ 'Manjeets' , 'Akashs' , 'Akshats' , 'Nikhils' ]
print ( "The original list : " + str (test_list))
K = 4
res = []
for i in test_list:
i = i.replace(i[ - K:],"")
res.append(i)
print ( "The list after removing last characters : " + str (res))
|
Output
The original list : ['Manjeets', 'Akashs', 'Akshats', 'Nikhils']
The list after removing last characters : ['Manj', 'Ak', 'Aks', 'Nik']
Time complexity: O(N * K), where N is the number of elements in the list and K is the number of characters to be removed from the end of each string.
Auxiliary Space: O(N), where N is the number of elements in the list.
Method#4: Using recursive method
Python3
def remove_rear_k_chars(test_list, k, res = []):
if not test_list:
return res
res.append(test_list[ 0 ][: len (test_list[ 0 ]) - k])
return remove_rear_k_chars(test_list[ 1 :], k, res)
test_list = [ 'Manjeets' , 'Akashs' , 'Akshats' , 'Nikhils' ]
print ( "The original list : " + str (test_list))
K = 4
res = remove_rear_k_chars(test_list, K)
print ( "The list after removing last characters : " + str (res))
|
Output
The original list : ['Manjeets', 'Akashs', 'Akshats', 'Nikhils']
The list after removing last characters : ['Manj', 'Ak', 'Aks', 'Nik']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#5: using re module
Python3
import re
test_list = [ 'Manjeets' , 'Akashs' , 'Akshats' , 'Nikhils' ]
print ( "The original list : " + str (test_list))
K = 4
res = [re.sub( '.{' + str (K) + '}$' , '', sub) for sub in test_list]
print ( "The list after removing last characters : " + str (res))
|
Output
The original list : ['Manjeets', 'Akashs', 'Akshats', 'Nikhils']
The list after removing last characters : ['Manj', 'Ak', 'Aks', 'Nik']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #9: Using the replace() method
- Initialize a list of strings test_list with some values.
- Print the original list using the print() function and the str() function to convert the list to a string.
- Initialize a variable K with the number of characters to remove from each string.
- Loop over the list of strings using the for loop and range() function.
- In each iteration, remove the last K characters from the current string using the replace() method and assigning the modified string back to the same index in the list.
- Print the modified list using the print() function and the str() function to convert the list to a string.
Python3
test_list = [ 'Manjeets' , 'Akashs' , 'Akshats' , 'Nikhils' ]
print ( "The original list : " + str (test_list))
K = 4
for i in range ( len (test_list)):
test_list[i] = test_list[i].replace(test_list[i][ - K:], '')
print ( "The list after removing last characters : " + str (test_list))
|
Output
The original list : ['Manjeets', 'Akashs', 'Akshats', 'Nikhils']
The list after removing last characters : ['Manj', 'Ak', 'Aks', 'Nik']
Time complexity: O(n * k), where n is the length of the list and k is the number of characters to remove from each string.
Auxiliary space: O(1), as we are modifying the input list in place and not using any extra space.
Similar Reads
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 given character from Strings list
Sometimes, while working with Python list, we can have a problem in which we need to remove a particular character from each string from list. This kind of application can come in many domains. Let's discuss certain ways to solve this problem. Method #1 : Using replace() + enumerate() + loop This is
8 min read
Remove Multiple Characters from a String in Python
Removing multiple characters from a string in Python can be achieved using various methods, such as str.replace(), regular expressions, or list comprehensions. Each method serves a specific use case, and the choice depends on your requirements. Letâs explore the different ways to achieve this in det
3 min read
Remove Special Characters from String in Python
When working with text data in Python, it's common to encounter strings containing unwanted special characters such as punctuation, symbols or other non-alphanumeric elements. For example, given the input "Data!@Science#Rocks123", the desired output is "DataScienceRocks123". Let's explore different
2 min read
Python - Remove Non-English characters Strings from List
Given a List of Strings, perform removal of all Strings with non-english characters. Input : test_list = ['Good| ????', '??Geeks???'] Output : [] Explanation : Both contain non-English characters Input : test_list = ["Gfg", "Best"] Output : ["Gfg", "Best"] Explanation : Both are valid English words.
8 min read
Python - Remove Initial character in String List
In Python, Sometimes, we come across an issue in which we require to delete the initial character from each string, that we might have added by mistake and we need to extend this to the whole list. This type of utility is common in web development. Having shorthands to perform this particular job is
7 min read
Python | Remove last character in list of strings
Sometimes, we come across an issue in which we require to delete the last character from each string, that we might have added by mistake and we need to extend this to the whole list. This type of utility is common in web development. Having shorthands to perform this particular job is always a plus
8 min read
Python - Remove front K characters from each string in String List
Sometimes, we come across an issue in which we require to delete the first K characters from each string, that we might have added by mistake and we need to extend this to the whole list. This type of utility is common in web development. Having shorthands to perform this particular job is always a
6 min read
Python program to remove last N characters from a string
In this article, weâll explore different ways to remove the last N characters from a string in Python. This common string manipulation task can be achieved using slicing, loops, or built-in methods for efficient and flexible solutions. Using String SlicingString slicing is one of the simplest and mo
2 min read
Python - Remove K length Duplicates from String
To remove consecutive K-length duplicates from a string iterate through the string comparing each substring with the next and excluding duplicates. For example we are given a string s = "abcabcabcabc" we need to remove k length duplicate from the string so that the output should become "aaaabc" . We
3 min read