Open In App

Python – Remove Rear K characters from String List

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# Remove Rear K characters from String List
# using list comprehension + list slicing
 
# initializing list
test_list = ['Manjeets', 'Akashs', 'Akshats', 'Nikhils']
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 4
 
# using list comprehension + list slicing
# Remove Rear K characters from String List
res = [sub[: len(sub) - K] for sub in test_list]
 
# printing result
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




# Python3 code to demonstrate
# Remove Rear K characters from String List
# using map() + lambda
 
# initializing list
test_list = ['Manjeets', 'Akashs', 'Akshats', 'Nikhils']
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 4
 
# using map() + lambda
# Remove Rear K characters from String List
res = list(map(lambda i: i[: (len(i) - K)], test_list))
 
# printing result
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




# Python3 code to demonstrate
# Remove Rear K characters from String List
 
# initializing list
test_list = ['Manjeets', 'Akashs', 'Akshats', 'Nikhils']
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 4
res=[]
for i in test_list:
    i=i.replace(i[-K:],"")
    res.append(i)
 
# printing result
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




# Python3 code to demonstrate
# Remove Rear K characters from String List
#using recursive method
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)
 
# initializing list
test_list = ['Manjeets', 'Akashs', 'Akshats', 'Nikhils']
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 4
 
 
# Remove Rear K characters from String List using recursive function
res = remove_rear_k_chars(test_list, K)
 
# printing result
print("The list after removing last characters : " + str(res))
#this code contributed by tvsk


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
 
# initializing list
test_list = ['Manjeets', 'Akashs', 'Akshats', 'Nikhils']
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 4
 
# using regular expressions
# Remove Rear K characters from String List
res = [re.sub('.{' + str(K) + '}$', '', sub) for sub in test_list]
 
# printing result
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

  1. Initialize a list of strings test_list with some values.
  2. Print the original list using the print() function and the str() function to convert the list to a string.
  3. Initialize a variable K with the number of characters to remove from each string.
  4. Loop over the list of strings using the for loop and range() function.
  5. 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.
  6. Print the modified list using the print() function and the str() function to convert the list to a string.

Python3




# Python3 code to demonstrate
# Remove Rear K characters from String List
# using the replace() method
 
# initializing list
test_list = ['Manjeets', 'Akashs', 'Akshats', 'Nikhils']
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 4
 
# Remove Rear K characters from String List using the replace() method
for i in range(len(test_list)):
    test_list[i] = test_list[i].replace(test_list[i][-K:], '')
 
# printing result
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.



Last Updated : 06 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads