Open In App

Python – Substring concatenation by Separator

Last Updated : 01 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Substring concatenation by Separator
# Using loop
 
# initializing list
test_list = ['gfg', 'is', '*', 'best', '*', 'for', 'geeks']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = '*'
 
# Substring concatenation by Separator
# Using loop
res = []
temp = ''
for sub in test_list:
    if sub != '*':
        temp += sub
    else:
        res.append(temp)
        temp = ''
if temp:
    res.append(temp)
 
# printing result
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




# Python3 code to demonstrate working of
# Substring concatenation by Separator
# Using join() + split() + list comprehension
 
# initializing list
test_list = ['gfg', 'is', '*', 'best', '*', 'for', 'geeks']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = '*'
 
# Substring concatenation by Separator
# Using join() + split() + list comprehension
res = [ele for ele in ''.join(test_list).split(K) if ele]
 
# printing result
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

  1. Import the re module for regular expressions.
  2. Initialize an empty list result.
  3. Join all the elements in test_list using an empty string as separator and store the result in a variable joined_str.
  4. Use the re.split() method to split the joined string by the separator K. This method returns a list of substrings.
  5. 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.
  6. Print the result list.

Python3




import re
 
# initializing list
test_list = ['gfg', 'is', '*', 'best', '*', 'for', 'geeks']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = '*'
 
# Substring concatenation by Separator
# Using regular expressions
joined_str = ''.join(test_list)
result = [s for s in re.split(re.escape(K), joined_str) if s]
 
# printing result
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




# Python3 code to demonstrate working of
# Substring concatenation by Separator
# Using loop
 
from functools import reduce
 
# initializing list
test_list = ['gfg', 'is', '*', 'best', '*', 'for', 'geeks']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = '*'
 
# Substring concatenation by Separator
# Using itertools.groupby()
res = [''.join(list(g)) for k, g in groupby(test_list, lambda x: x == K) if not k]
 
# printing result
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
Previous
Next
Share your thoughts in the comments

Similar Reads