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.
Share your thoughts in the comments
Please Login to comment...