Open In App

Python | Convert String list to Joined Single element

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python, we can have a problem in which we need to perform the task of joining each element of String list to a single element in List by combining using delim. This kind of application can come in web development domain. Lets discuss certain ways in which this task can be performed. 

Method #1 : Using loop

This is one of way to perform this task. In this, we iterate for the String list for strings using loop and perform the join operation according to the delim. 

Python3




# Python3 code to demonstrate working of
# Convert String list to Joined Single element
# Using loop
 
# initializing list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing delim
delim = "-"
 
# Convert String list to Joined Single element
# Using loop
res = ''
for idx in range(len(test_list)-1):
    res = res + test_list[idx] + delim
 
  # if list is greater than 0
if len(test_list) > 0:
    res = res + test_list[-1]
res = [res]
 
# printing result
print("String after performing join : " + str(res))


Output : 

The original list is : ['gfg', 'is', 'best']
String after performing join : ['gfg-is-best']

Time Complexity: O(n), where n is the length of the input list. This is because we’re using the loop which has a time complexity of O(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 #2: Using join() + list comprehension

This task can also be performed using a combination of the above functions. In this, we perform the task of combining using join(). The logic is compiled using list comprehension.

Python3




# Python3 code to demonstrate working of
# Convert String list to Joined Single element
# Using join() + list comprehension
 
# initializing list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing delim
delim = "-"
 
# Convert String list to Joined Single element
# Using join() + list comprehension
res = [delim.join(test_list)]
 
# printing result
print("String after performing join : " + str(res))


Output : 

The original list is : ['gfg', 'is', 'best']
String after performing join : ['gfg-is-best']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3 : Using reduce() + add()

This method makes use of the reduce function from functools module to apply a binary function cumulatively on the elements of the given list. The binary function used here is add() from operator module.

Python3




# Python3 code to demonstrate working of
# Convert String list to Joined Single element
# Using reduce() + add()
 
# importing reduce from functools
from functools import reduce
 
# importing add from operator
from operator import add
 
# initializing list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing delim
delim = "-"
 
# Convert String list to Joined Single element
# Using reduce() + add()
res = reduce(add, [x + delim for x in test_list[:-1]] + [test_list[-1]])
res = [res]
 
# printing result
print("String after performing join : " + str(res))


Output

The original list is : ['gfg', 'is', 'best']
String after performing join : ['gfg-is-best']

Time Complexity: O(n), where n is the number of elements in the list.
Auxiliary Space: O(n), where n is the length of the resulting string after join operation.

Method 4 : Using itertools.chain() and join() functions

Python3




import itertools
 
# initializing list
test_list = ['gfg', 'is', 'best']
 
# initializing delimiter
delim = "-"
 
# using itertools.chain() to concatenate the elements of the list with delimiter
concatenated_list = itertools.chain.from_iterable(
    (s, delim) for s in test_list[:-1])
 
# concatenating the concatenated list with the last element of the original list
joined_string = ''.join(concatenated_list) + test_list[-1]
 
# creating a list with the joined string as the only element
res = [joined_string]
 
# printing result
print("String after performing join: " + str(res))


Output

String after performing join: ['gfg-is-best']

Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), as we are creating an intermediate concatenated list of length n.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads