Python – Remove Initial character in String List
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.
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
# Python3 code to demonstrate # Remove Initial character in String List # using list comprehension + list slicing # initializing list test_list = [ 'Amanjeet' , 'sakash' , 'kakshat' , 'Knikhil' ] # printing original list 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] # printing result print ( "The list after removing initial characters : " + str (res)) |
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.
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 initial element using list comprehension.
Python3
# Python3 code to demonstrate # Remove Initial character in String List # using map() + lambda # initializing list test_list = [ 'Amanjeet' , 'sakash' , 'kakshat' , 'Knikhil' ] # printing original list print ( "The original list : " + str (test_list)) # using map() + lambda # Remove Initial character in String List res = list ( map ( lambda i: i[ 1 :], test_list)) # printing result print ( "The list after removing initial characters : " + str (res)) |
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’.
Method #3: Using replace() method
Python3
# Python3 code to demonstrate # Remove Initial character in String List # initializing list 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) # printing result print ( "The list after removing initial characters : " + str (res)) |
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.
Method #4 : Using pop() and join() methods
Python3
# Python3 code to demonstrate # Remove Initial character in String List # initializing list 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: if len (i) = = 0 : res.append(i) else : x = list (i) x.pop( 0 ) res.append("".join(x)) # printing result print ( "The list after removing initial characters : " + str (res)) |
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.
Method #5 : Using filter() + lambda
Python3
# Python3 code to demonstrate # Remove Initial character in String List # using filter() + lambda # initializing list test_list = [ 'Amanjeet' , 'sakash' , 'kakshat' , 'Knikhil' ] # printing original list print ( "The original list : " + str (test_list)) # using filter() + lambda # Remove Initial character in String List res = list ( filter ( lambda i: i[ 1 :], test_list)) # printing result print ( "The list after removing initial characters : " + str (res)) |
The original list : ['Amanjeet', 'sakash', 'kakshat', 'Knikhil'] The list after removing initial characters : ['Amanjeet', 'sakash', 'kakshat', 'Knikhil']
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.
Method #6: Using Regular expressions.
Step-by-step approach:
- Initialize an empty list res.
- Iterate over each string s in the input list test_list.
- Use re.sub() method to remove the first character of the string s using the regular expression ‘^.’.
- Append the modified string to the res list.
- Return the final modified list
Python3
# Python3 code to demonstrate # Remove Initial character in String List # using regular expressions import re # initializing list test_list = [ 'Amanjeet' , 'sakash' , 'kakshat' , 'Knikhil' ] # printing original list print ( "The original list : " + str (test_list)) # using regular expressions # Remove Initial character in String List res = [re.sub(r '^.' , '', s) for s in test_list] # printing result print ( "The list after removing initial characters : " + str (res)) |
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.
Method #7: Using reduce
In this method, we use 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
# Python3 code to demonstrate # Remove Initial character in String List # using reduce() + lambda function # importing reduce() function from functools module from functools import reduce # initializing list test_list = [ 'Amanjeet' , 'sakash' , 'kakshat' , 'Knikhil' ] # printing original list print ( "The original list : " + str (test_list)) # using reduce() + lambda function # Remove Initial character in String List res = reduce ( lambda acc, x: acc + [x[ 1 :]], test_list, []) # printing result print ( "The list after removing initial characters : " + str (res)) |
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.
Please Login to comment...