Sometimes we face a problem in which we have a list of strings and there are some garbage/unwanted letters at its prefix or suffix or at the specified position uniformly, i.e this extends to all the strings in the list. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using list comprehension + list slicing This task can be performed using list comprehension and list slicing. List slicing can be used to remove the unwanted letters and list comprehension can be used to extend the logic to the entire string.
Python3
# Python3 code to demonstrate # Split strings in list # Using list comprehension + list slicing # initializing list test_list = [ 'Rs.25' , 'Rs.100' , 'Rs.143' , 'Rs.12' , 'Rs.4010' ] # printing original list print ("The original list : " + str (test_list)) # using list comprehension + list slicing # Split strings in list res = [sub[ 3 :] for sub in test_list] # print result print ("The list after string slicing : " + str (res)) |
The original list : ['Rs.25', 'Rs.100', 'Rs.143', 'Rs.12', 'Rs.4010'] The list after string slicing : ['25', '100', '143', '12', '4010']
Time complexity: O(n), where n is the length of the test_list. The list comprehension + list slicing takes O(n) time
Auxiliary Space: O(n), where n is the number of elements in the list test_list
Method #2 : Using map() + slicing + lambda This particular task can be performed using map function as well. The task of performing the same for each string is handled by lambda function and map function.
Python3
# Python3 code to demonstrate # Split strings in list # Using map() + slicing + lambda # initializing list test_list = [ 'Rs.25' , 'Rs.100' , 'Rs.143' , 'Rs.12' , 'Rs.4010' ] # printing original list print ("The original list : " + str (test_list)) # using map() + slicing + lambda # Split strings in list res = list ( map ( lambda sub: sub[ 3 :], test_list)) # print result print ("The list after string slicing : " + str (res)) |
The original list : ['Rs.25', 'Rs.100', 'Rs.143', 'Rs.12', 'Rs.4010'] The list after string slicing : ['25', '100', '143', '12', '4010']
Method #3 : Using re
Using regular expressions is another approach for splitting strings in a list with the same prefix in all elements. Regular expressions, or regex for short, are a way to specify patterns in strings. They can be used to search, edit, or manipulate text.
To use regular expressions for this task, you can use the re module, which provides functions and classes for working with regular expressions in Python.
Here is an example of how you can use regular expressions to split strings in a list with the same prefix in all elements:
Python3
import re # initializing list test_list = [ 'Rs.25' , 'Rs.100' , 'Rs.143' , 'Rs.12' , 'Rs.4010' ] # printing original list print ( "The original list:" , test_list) # using regular expressions pattern = re. compile (r "Rs\." ) # compile the regex pattern res = [pattern.sub("", elem) for elem in test_list] # use the sub() method to remove the prefix # print result print ( "The list after string slicing:" , res) #This code is contributed by Edula Vinay Kumar Reddy |
The original list: ['Rs.25', 'Rs.100', 'Rs.143', 'Rs.12', 'Rs.4010'] The list after string slicing: ['25', '100', '143', '12', '4010']
The time and space complexity of the regular expression approach for splitting strings in a list with the same prefix in all elements will depend on the specific implementation and the size of the input.
In general, the time complexity of regular expression matching can be O(n) or O(n * m) in the worst case, where “n” is the size of the input string and “m” is the size of the regex pattern. This is because the regex engine may need to try every possible combination of the pattern and the input to find a match. However, in practice, most regex patterns and inputs will have a much lower time complexity, and the actual time taken to match a regex pattern can depend on various factors such as the structure of the pattern, the type and number of characters in the input, and the performance of the regex engine.
The space complexity of the regular expression approach will be O(n)
Method #4: Using string method removeprefix()
Python 3.9 introduced a new method called removeprefix() to remove a prefix from a string. This method returns a copy of the string with the prefix removed. We can use this method to remove the same prefix from all the strings in a list.
Note: This method only works in Python 3.9 or above. It wont work in GFG compiler
Python3
#Python3 code to demonstrate #Split strings in list #Using string method removeprefix() #initializing list test_list = [ 'Rs.25' , 'Rs.100' , 'Rs.143' , 'Rs.12' , 'Rs.4010' ] #printing original list print ( "The original list:" , test_list) #using removeprefix() method res = [elem.removeprefix( "Rs." ) for elem in test_list] #print result print ( "The list after string slicing:" , res) |
Output:
The original list: [‘Rs.25’, ‘Rs.100’, ‘Rs.143’, ‘Rs.12’, ‘Rs.4010’]
The list after string slicing: [’25’, ‘100’, ‘143’, ’12’, ‘4010’]
Time complexity: O(n), where n is the length of the test_list. The list comprehension + removeprefix() method takes O(n) time.
Auxiliary Space: O(n), where n is the number of elements in the list test_list. We are creating a new list to store the modified strings.