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
# 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:
- 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
# 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))
OutputThe 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'.
Similar Reads
Python | Split strings and digits from string list Sometimes, while working with String list, we can have a problem in which we need to remove the surrounding stray characters or noise from list of digits. This can be in form of Currency prefix, signs of numbers etc. Let's discuss a way in which this task can be performed. Method #1 : Using list com
5 min read
Splitting String to List of Characters - Python We are given a string, and our task is to split it into a list where each element is an individual character. For example, if the input string is "hello", the output should be ['h', 'e', 'l', 'l', 'o']. Let's discuss various ways to do this in Python.Using list()The simplest way to split a string in
2 min read
Splitting String to List of Characters - Python The task of splitting a string into a list of characters in Python involves breaking down a string into its individual components, where each character becomes an element in a list. For example, given the string s = "GeeksforGeeks", the task is to split the string, resulting in a list like this: ['G
3 min read
Tokenizing Strings in List of Strings - Python The task of tokenizing strings in a list of strings in Python involves splitting each string into smaller units, known as tokens, based on specific delimiters. For example, given the list a = ['Geeks for Geeks', 'is', 'best computer science portal'], the goal is to break each string into individual
2 min read
Python program to split a string by the given list of strings Given a list of strings. The task is to split the string by the given list of strings. Input : test_str = 'geekforgeeksbestforgeeks', sub_list = ["best"] Output : ['geekforgeeks', 'best', 'forgeeks'] Explanation : "best" is extracted as different list element. Input : test_str = 'geekforgeeksbestfor
4 min read
Python | Split flatten String List Sometimes, while working with Python Strings, we can have problem in which we need to perform the split of strings on a particular deliminator. In this, we might need to flatten this to a single String List. Let's discuss certain ways in which this task can be performed. Method #1 : Using list compr
7 min read