Python | Splitting string list by strings
Last Updated :
17 Apr, 2023
Sometimes, while working with Python strings, we might have a problem in which we need to perform a split on a string. But we can have a more complex problem of having a front and rear string and need to perform a split on them. This can be multiple pairs for split. Let’s discuss certain way to solve this particular problem.
Method : Using loop + index() + list slicing
This task can be performed by using the above functionalities together. In this, we just loop along the pairs and get the desired indices using index(). Then list slicing is done to construct a new list of the desired slice and appended to form a new resultant list.
Python3
test_list = [ 'gfg' , 'is' , 'best' , "for" , 'CS' , 'and' , 'Maths' ]
split_list = [( 'gfg' , 'best' ), ( 'CS' , 'Maths' )]
print ( "The original list is : " + str (test_list))
print ( "The split list is : " + str (split_list))
for start, end in split_list:
temp1 = test_list.index(start)
temp2 = test_list.index(end) + 1
test_list[temp1: temp2] = [test_list[temp1: temp2]]
print ( "The resultant split list is : " + str (test_list))
|
Output :
The original list is : ['gfg', 'is', 'best', 'for', 'CS', 'and', 'Maths']
The split list is : [('gfg', 'best'), ('CS', 'Maths')]
The resultant split list is : [['gfg', 'is', 'best'], 'for', ['CS', 'and', 'Maths']]
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list
Method #2: Using a list comprehension + enumerate() with nested ternary operators
Step-by-step algorithm:
- Initialize an empty list ‘temp’ to store the split string lists.
- Iterate over each element ‘x’ and its index ‘i’ in the ‘test_list’.
- For each element ‘x’, check if any pair of the form (x, y) is present in the ‘split_list’ such that the index of ‘y’ is greater than or equal to the index of ‘x’ in ‘test_list’. If such a pair exists, slice the ‘test_list’ from index ‘i’ to the index of ‘y’ and append it to ‘temp’. If no such pair exists, append ‘x’ to ‘temp’.
- Return the list ‘temp’.
Python3
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'CS' , 'and' , 'Maths' ]
split_list = [( 'gfg' , 'best' ), ( 'CS' , 'Maths' )]
print ( "The original list is : " + str (test_list))
print ( "The split list is : " + str (split_list))
test_list = [test_list[i:j + 1 ] if (test_list[i], test_list[j]) in split_list else [x] if i = = j else [] for i, x in enumerate (test_list) for j, y in enumerate (test_list) if (x,y) in split_list and i< = j]
test_list = [x for x in test_list if x]
print ( "The resultant split list is : " + str (test_list))
|
Output
The original list is : ['gfg', 'is', 'best', 'for', 'CS', 'and', 'Maths']
The split list is : [('gfg', 'best'), ('CS', 'Maths')]
The resultant split list is : [['gfg', 'is', 'best'], ['CS', 'and', 'Maths']]
Time Complexity: O(n), where ‘n’ is the length of the ‘test_list’.
Space Complexity: O(n), where ‘n’ is the length of the ‘test_list’.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...