Python – Substring concatenation by Separator
Last Updated :
01 Jun, 2023
Sometimes, while working with Python Lists, we can have problem in which we need to perform the concatenation of strings in a list till the separator. This can have application in domains in which we need chunked data. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop This is brute force way in which this task can be performed. In this, we perform the task of joining in loop conditional statements and re initialize string to empty on separator occurrence.
Python3
test_list = [ 'gfg' , 'is' , '*' , 'best' , '*' , 'for' , 'geeks' ]
print ( "The original list is : " + str (test_list))
K = '*'
res = []
temp = ''
for sub in test_list:
if sub ! = '*' :
temp + = sub
else :
res.append(temp)
temp = ''
if temp:
res.append(temp)
print ( "The list after String concatenation : " + str (res))
|
Output :
The original list is : ['gfg', 'is', '*', 'best', '*', 'for', 'geeks']
The list after String concatenation : ['gfgis', 'best', 'forgeeks']
Time Complexity: O(n) where n is the total number of values in the list “test_list”.
Auxiliary Space: O(n) where n is the total number of values in the list “test_list”.
Method #2 : Using join() + split() + list comprehension The combination of above functions can be used to perform this task. In this, we perform the task of concatenation using join(). The list construction is done using list comprehension.
Python3
test_list = [ 'gfg' , 'is' , '*' , 'best' , '*' , 'for' , 'geeks' ]
print ("The original list is : " + str (test_list))
K = '*'
res = [ele for ele in ''.join(test_list).split(K) if ele]
print ("The list after String concatenation : " + str (res))
|
Output :
The original list is : ['gfg', 'is', '*', 'best', '*', 'for', 'geeks']
The list after String concatenation : ['gfgis', 'best', 'forgeeks']
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using the using join() + split() + list comprehension which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #5: Using regular expressions
- Import the re module for regular expressions.
- Initialize an empty list result.
- Join all the elements in test_list using an empty string as separator and store the result in a variable joined_str.
- Use the re.split() method to split the joined string by the separator K. This method returns a list of substrings.
- Use a list comprehension to iterate over the list of substrings and remove any empty strings. Append each non-empty substring to the result list.
- Print the result list.
Python3
import re
test_list = [ 'gfg' , 'is' , '*' , 'best' , '*' , 'for' , 'geeks' ]
print ( "The original list is : " + str (test_list))
K = '*'
joined_str = ''.join(test_list)
result = [s for s in re.split(re.escape(K), joined_str) if s]
print ( "The list after String concatenation : " + str (result))
|
Output
The original list is : ['gfg', 'is', '*', 'best', '*', 'for', 'geeks']
The list after String concatenation : ['gfgis', 'best', 'forgeeks']
Time complexity: O(n)
Auxiliary space: O(n) (for the result list)
Method #6: Using itertools.groupby()
In this method, groupby() groups the consecutive elements based on the separator (K) passed to the lambda function. k is True for the separator, so we check if not k to filter out the separators. list(g) contains the elements between the separators, which we join using ”.join() to form a string. Finally, we append the resulting string to the list res.
Python3
from functools import reduce
test_list = [ 'gfg' , 'is' , '*' , 'best' , '*' , 'for' , 'geeks' ]
print ( "The original list is : " + str (test_list))
K = '*'
res = [''.join( list (g)) for k, g in groupby(test_list, lambda x: x = = K) if not k]
print ( "The list after String concatenation : " + str (res))
|
Output:
The original list is : ['gfg', 'is', '*', 'best', '*', 'for', 'geeks']
The list after String concatenation : ['gfgis', 'best', 'forgeeks']
Time complexity: O(n), where n is the length of the input list. This is because we are iterating through the list only once, and each operation performed inside the loop takes constant time.
Auxiliary space: O(n), as we are creating a new list to store the concatenated substrings.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...