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. 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 whole list.
Python3
test_list = [ 'Manjeets' , 'Akashs' , 'Akshats' , 'Nikhils' ]
print ( "The original list : " + str (test_list))
res = [sub[: - 1 ] 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 : ['Manjeet', 'Akash', 'Akshat', 'Nikhil']
Time complexity: O(n), where n is the number of elements in the list.
Auxiliary Space: O(n), as the result list will store the new modified elements which are the substrings of the original list elements.
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 element using list comprehension.
Python3
test_list = [ 'Manjeets' , 'Akashs' , 'Akshats' , 'Nikhils' ]
print ( "The original list : " + str (test_list))
res = list ( map ( lambda i: i[: - 1 ], 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 : ['Manjeet', 'Akash', 'Akshat', 'Nikhil']
Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(n), as a new list is created to store the result of the operation.
Method #3 : Using pop() and join() methods
Python3
test_list = [ 'Manjeets' , 'Akashs' , 'Akshats' , 'Nikhils' ]
print ( "The original list : " + str (test_list))
res = []
for i in test_list:
x = list (i)
if ( len (x) = = 0 ):
res.append(i)
else :
x.pop()
res.append("".join(x))
print ( "The list after removing last characters : " + str (res))
|
Output
The original list : ['Manjeets', 'Akashs', 'Akshats', 'Nikhils']
The list after removing last characters : ['Manjeet', 'Akash', 'Akshat', 'Nikhil']
Time complexity: O(n * m), where n is the length of the input list and m is the maximum length of a string in the list.
Auxiliary space: O(n * m), because we are creating a new list of the same length as the input list and also creating a new list for each string in the input list.
Method #4: Using rsplit()
The rsplit() method splits a string from the right, starting at the last character. By passing 1 as the only argument, it returns a list with the original string as the first element, and the last character as the second element. Then, we use string slicing to remove the last character and store the modified strings in the res list.
Python3
test_list = [ 'Manjeets' , 'Akashs' , 'Akshats' , 'Nikhils' ]
print ( "The original list:" , test_list)
res = [string[: - 1 ] if string else string for string in [string.rsplit( ' ' , 1 )[ 0 ] for string in test_list]]
print ( "The list after removing last characters:" , res)
|
Output
The original list: ['Manjeets', 'Akashs', 'Akshats', 'Nikhils']
The list after removing last characters: ['Manjeet', 'Akash', 'Akshat', 'Nikhil']
Time complexity: O(n)
Auxiliary Space: O(n)
Method#5: Using Recursive method.
Algorithm:
- Define a function remove_last_char that takes a list of strings as input.
- Check if the input list is empty, if yes then return an empty list.
- Otherwise, get the first string in the input list and remove its last character.
- Recursively call the remove_last_char function on the rest of the input list.
- Combine the modified first string with the modified rest of the input list and return it.
Python3
def remove_last_char(lst):
if not lst:
return []
else :
first = lst[ 0 ]
new_first = first[: - 1 ]
rest = remove_last_char(lst[ 1 :])
return [new_first] + rest
test_list = [ 'Manjeets' , 'Akashs' , 'Akshats' , 'Nikhils' ]
print ( "The original list : " + str (test_list))
res = remove_last_char(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 : ['Manjeet', 'Akash', 'Akshat', 'Nikhil']
Time complexity: O(nm), where n is the number of strings in the input list and m is the length of the longest string. This is because we are looping through the input list once and calling the [: -1] operation on each string, which takes O(m) time.
Auxiliary space: O(nm), because we are creating a new list of modified strings of the same length as the input list. However, this algorithm has a recursive structure, so it may use additional space on the call stack.
Method #6: Using loop iteration and string slicing
Step-by-step approach:
- Initialize an empty list to store the modified strings.
- Use a loop to iterate over each string in the given list.
- Slice the string to remove the last character using string slicing.
- Append the modified string to the empty list.
- Return the modified list of strings.
Below is the implementation of the above approach:
Python3
test_list = [ 'Manjeets' , 'Akashs' , 'Akshats' , 'Nikhils' ]
print ( "The original list : " + str (test_list))
res = []
for sub in test_list:
modified_sub = sub[: - 1 ]
res.append(modified_sub)
print ( "The list after removing last characters : " + str (res))
|
Output
The original list : ['Manjeets', 'Akashs', 'Akshats', 'Nikhils']
The list after removing last characters : ['Manjeet', 'Akash', 'Akshat', 'Nikhil']
Time complexity: O(n), where n is the number of strings in the list.
Auxiliary space: O(n), where n is the number of strings in the list.
Method #8: Using regular expressions
Import the re module for regular expressions.
Define a regular expression pattern that matches the last character in a string.
Use the re.sub() function to replace the last character in each string in the input list with an empty string using the regular expression pattern.
Return the modified list.
Python3
import re
test_list = [ 'Manjeets' , 'Akashs' , 'Akshats' , 'Nikhils' ]
print ( "The original list : " + str (test_list))
pattern = '.$'
res = [re.sub(pattern, '', s) for s 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 : ['Manjeet', 'Akash', 'Akshat', 'Nikhil']
Time complexity: O(n)
Auxiliary space: O(n)
Method 8: Using the [:-1] slice notation.
Python3
test_list = [ 'Manjeets' , 'Akashs' , 'Akshats' , 'Nikhils' ]
print ( "The original list: " + str (test_list))
res = [s[: - 1 ] for s 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: ['Manjeet', 'Akash', 'Akshat', 'Nikhil']
Time complexity: O(n), where n is the number of strings in the list.
Auxiliary space: O(n), as we create a new list res to store the modified strings.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 May, 2023
Like Article
Save Article