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. 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 code to demonstrate # remove last character from list of strings # using list comprehension + list slicing # initializing list test_list = [ 'Manjeets' , 'Akashs' , 'Akshats' , 'Nikhils' ] # printing original list print ( "The original list : " + str (test_list)) # using list comprehension + list slicing # remove last character from list of strings res = [sub[ : - 1 ] for sub in test_list] # printing result print ( "The list after removing last characters : " + str (res)) |
The original list : ['Manjeets', 'Akashs', 'Akshats', 'Nikhils'] The list after removing last characters : ['Manjeet', 'Akash', 'Akshat', 'Nikhil']
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 code to demonstrate # remove last character from list of strings # using map() + lambda # initializing list test_list = [ 'Manjeets' , 'Akashs' , 'Akshats' , 'Nikhils' ] # printing original list print ( "The original list : " + str (test_list)) # using map() + lambda # remove last character from list of strings res = list ( map ( lambda i: i[ : - 1 ], test_list)) # printing result print ( "The list after removing last characters : " + str (res)) |
The original list : ['Manjeets', 'Akashs', 'Akshats', 'Nikhils'] The list after removing last characters : ['Manjeet', 'Akash', 'Akshat', 'Nikhil']
Recommended Posts:
- Python | Remove given character from Strings list
- Python | Remove Kth character from strings list
- Python | Remove empty strings from list of strings
- Python | Remove tuple from list of tuples if not containing any character
- Python | Remove all digits from a list of strings
- Python | Remove prefix strings from list
- Python | Remove all strings from a list of tuples
- Python | Pair the consecutive character strings in a list
- Python | Remove trailing/leading special characters from strings list
- Ways to remove i'th character from string in Python
- Python | Convert string List to Nested Character List
- Python | Remove all values from a list present in other list
- Python | Strings with similar front and rear character
- Python | Tokenizing strings in list of strings
- Python | Filter list of strings based on the substring list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.