Open In App

Python | Splitting string list by strings

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Splitting string list by strings
# using loop + index() + list slicing
 
# initialize list
test_list = ['gfg', 'is', 'best', "for", 'CS', 'and', 'Maths']
 
# initialize split list
split_list = [('gfg', 'best'), ('CS', 'Maths')]
 
# printing original list
print("The original list is : " + str(test_list))
 
# printing split list
print("The split list is : " + str(split_list))
 
# Splitting string list by strings
# using loop + index() + list slicing
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]]
 
# printing result
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:

  1. Initialize an empty list ‘temp’ to store the split string lists.
  2. Iterate over each element ‘x’ and its index ‘i’ in the ‘test_list’.
  3. 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’.
  4. Return the list ‘temp’.

Python3




# initialize list
test_list = ['gfg', 'is', 'best', 'for', 'CS', 'and', 'Maths' ]
 
# initialize split list
split_list = [('gfg', 'best'), ('CS', 'Maths')]
 
# printing original list
print("The original list is : " + str(test_list))
 
# printing split list
print("The split list is : " + str(split_list))
 
# Splitting string list by strings using nested ternary operators
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]
 
# Remove empty lists
test_list = [x for x in test_list if x]
 
# printing result
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
Previous
Next
Share your thoughts in the comments

Similar Reads