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 always a plus. Let’s discuss certain ways in which this can be achieved.
Input: [' Kashmita ', 'kaman', 'sakash', 'sarpita'] Output: [' ashmita ', 'aman', 'akash', 'arpita'] Explanation: In this, we are removing the initial character in the string list.
Remove First Character from String in Python
In Python, there are different methods to remove the first character from the string. Here are some methods listed below:
- Using replace() method
- Using reduce () Method
- Using list comprehension
- Using Regular expressions
- Using map() and lambda
- Using pop() and join() methods
- Using filter()
Remove String Initial Character using Replace() method
In Python, you can remove the first character from the string using the replace()
method. As the replace()
method is used to replace occurrences of a substring within a string, it can also be used to remove a specific character or substring by replacing it with an empty string.
Python3
test_list = [ 'Amanjeet' , 'sakash' , 'kakshat' , 'Knikhil' ] # printing original list print ( "The original list : " + str (test_list)) # Remove Initial character in String List res = [] for i in test_list: i = i.replace(i[ 0 ], "", 1 ) res.append(i) print ( "The list after removing initial characters : " + str (res)) |
Output
The original list : ['Amanjeet', 'sakash', 'kakshat', 'Knikhil']
The list after removing initial 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.
Remove String Initial Character using Reduce() method
In this method, we Remove the First Character from the String using reduce() and lambda function to apply the slicing operation to each element of the list to remove the initial character. The initial value of the accumulator is an empty list, and the result is stored in the list res. Finally, the result is printed using the print() function.
Python3
# importing reduce() function from functools module from functools import reduce test_list = [ 'Amanjeet' , 'sakash' , 'kakshat' , 'Knikhil' ] print ( "The original list : " + str (test_list)) res = reduce ( lambda acc, x: acc + [x[ 1 :]], test_list, []) print ( "The list after removing initial characters : " + str (res)) |
Output
The original list : ['Amanjeet', 'sakash', 'kakshat', 'Knikhil']The list after removing initial characters : ['manjeet', 'akash', 'akshat', 'nikhil']
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Delete
first character of a string using List Comprehension
In Python, you can remove first character from the slicing string using list comprehension and 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
# using list comprehension + list slicing test_list = [ 'Amanjeet' , 'sakash' , 'kakshat' , 'Knikhil' ] print ( "The original list : " + str (test_list)) # using list comprehension + list slicing # Remove Initial character in String List res = [sub[ 1 :] for sub in test_list] print ( "The list after removing initial characters : " + str (res)) |
Output
The original list : ['Amanjeet', 'sakash', 'kakshat', 'Knikhil']
The list after removing initial characters : ['manjeet', 'akash', 'akshat', 'nikhil']
Time Complexity: O(n), where n is the length of the test_list.
Auxiliary Space: O(n), as we are creating a new list “res” to store the modified strings, which takes up O(n) space in the worst-case scenario when all elements in the test_list are of the same length.
Remove String Initial Character using Regular expressions
In Python, you can remove first character from the string using regular expressions and the re
module. Regular expressions provide a powerful pattern-matching and manipulation mechanism that allows you to perform complex string operations.
Python3
# using regular expressions import re test_list = [ 'Amanjeet' , 'sakash' , 'kakshat' , 'Knikhil' ] print ( "The original list : " + str (test_list)) res = [re.sub(r '^.' , '', s) for s in test_list] print ( "The list after removing initial characters : " + str (res)) |
Output
The original list : ['Amanjeet', 'sakash', 'kakshat', 'Knikhil']
The list after removing initial characters : ['manjeet', 'akash', 'akshat', 'nikhil']
Time Complexity: The time complexity of this solution is O(nm), where n is the length of the input list and m is the maximum length of a string in the input list. This is because the regular expression operation has to be performed for each string in the list, and the time it takes to perform this operation depends on the length of the string.
Space Complexity: The space complexity of this solution is O(nm), where n is the length of the input list and m is the maximum length of a string in the input list. This is because we create a new list to store the modified strings, and the size of this list is proportional to the size of the input list and the length of the longest string in the input list.
Remove String Initial Character using map() and lambda
In Python, you can remove the first character from the string using map() and lambda. The map() function can perform the task of getting the functionality executed for all the members of the list and the lambda function performs the task of removal of initial element using list comprehension.
Python3
# initializing list test_list = [ 'Amanjeet' , 'sakash' , 'kakshat' , 'Knikhil' ] print ( "The original list : " + str (test_list)) res = list ( map ( lambda i: i[ 1 :], test_list)) print ( "The list after removing initial characters : " + str (res)) |
Output
The original list : ['Amanjeet', 'sakash', 'kakshat', 'Knikhil']
The list after removing initial characters : ['manjeet', 'akash', 'akshat', 'nikhil']
Time complexity: O(n), where n is the length of the list ‘test_list’. The code iterates through each element in the list and performs a simple string-slicing operation which takes O(1) time.
Auxiliary space: O(n), as a new list ‘res’ is created to store the result of the string slicing operation. The size of the ‘res’ list is the same as the size of the ‘test_list’.
Remove String Initial Character using pop() and join() methods
In Python, you can remove the first character from the string using the pop()
and
join()
methods. The pop()
method is used to remove an element at a specific index from a list, and the join()
method is used to concatenate the elements of a list into a single string.
Python3
test_list = [ 'Amanjeet' , 'sakash' , 'kakshat' , 'Knikhil' ] print ( "The original list : " + str (test_list)) # Remove Initial character in String List res = [] for i in test_list: if len (i) = = 0 : res.append(i) else : x = list (i) x.pop( 0 ) res.append("".join(x)) print ( "The list after removing initial characters : " + str (res)) |
Output
The original list : ['Amanjeet', 'sakash', 'kakshat', 'Knikhil']
The list after removing initial 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), as we are creating a new list and a new list for each string in the original list.
Remove String Initial Character using filter()
In Python, you can remove the first character from string using the filter()
function along with a lambda function. The filter()
function allows you to iterate over a sequence and filter out specific elements based on a provided condition. By using a lambda function with the filter()
function, you can define the condition for filtering elements.
Python3
test_list = [ 'Amanjeet' , 'sakash' , 'kakshat' , 'Knikhil' ] print ( "The original list : " + str (test_list)) res = list ( filter ( lambda i: i[ 1 :], test_list)) print ( "The list after removing initial characters : " + str (res)) |
Output
The original list : ['Amanjeet', 'sakash', 'kakshat', 'Knikhil']
The list after removing initial characters : ['manjeet', 'akash', 'akshat', 'nikhil']
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), as a new list is created to store the filtered elements.