Python | Remove last character in list of strings
Last Updated :
18 May, 2023
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.
Similar Reads
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 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
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
Split String into List of characters in Python
We are given a string and our task is to split this string into a list of its individual characters, this can happen when we want to analyze or manipulate each character separately. For example, if we have a string like this: 'gfg' then the output will be ['g', 'f', 'g']. Using ListThe simplest way
2 min read
Python - Remove Rear K characters from String List
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 plu
5 min read
Create a List of Strings in Python
Creating a list of strings in Python is easy and helps in managing collections of text. For example, if we have names of people in a group, we can store them in a list. We can create a list of strings by using Square Brackets [] . We just need to type the strings inside the brackets and separate the
3 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
Get Last N characters of a string - Python
We are given a string and our task is to extract the last N characters from it. For example, if we have a string s = "geeks" and n = 2, then the output will be "ks". Let's explore the most efficient methods to achieve this in Python. Using String Slicing String slicing is the fastest and most straig
2 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