Open In App

Python – Convert List to delimiter separated String

Improve
Improve
Like Article
Like
Save
Share
Report

In Python, lists are useful data structures that allow us to store collections of elements. Often, there arises a need to convert a list into a single string, where each element is separated by a delimiter of our choice. In this article, we will explore how to convert a list to a delimiter-separated string and demonstrate practical use cases for this operation. Given a List of elements, convert it to delimiter separated String.

Input: Test_list = [7, "Gfg", "best", 9], delim = "*" 
Output: 7*GFG*best*9* 
Explanation: All elements are concatenated with "*" as a joiner

Python List to Delimiter Separated String

Here are the different methods in Python list to string with commas.

  • Using reduce() function
  • Using list comprehension
  • Using loop
  • Using join()
  • Using map()
  • Using string formatting and unpacking

Convert a List to a Comma-Separated String using Reduce() function

In Python, we can convert List to Delimeter separated String using reduce(). This method uses reduce() function from the functools module to reduce the list into a single string by applying a lambda function that concatenates the elements with the delimiter.

Python3




from functools import reduce
 
test_list = [7, "Gfg", 8, "is", "best", 9]
delim = "*"
 
res = reduce(lambda x, y: str(x) + delim + str(y), test_list)
 
print("The resultant string : " + str(res))


Output

The resultant string : 7*Gfg*8*is*best*9

Time complexity: O(n), where n is the length of the input list. 
Auxiliary space:  O(n), where n is the length of the input list. 

Convert a List to a Comma-Separated String using list comprehension

In Python, we can convert List to Delimeter separated String using list comprehension. This program takes a list of mixed data types, converts each element to a string, and joins them with a specified delimiter to create a string. It does this using list comprehension and the join() method.

Python3




test_list = [7, "Gfg", 8, "is", "best", 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
delim = "*"
 
# using list comprehension to convert each element to string and adding delim
res = delim.join([str(ele) for ele in test_list])
 
print("The resultant string : " + str(res))


Output

The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
The resultant string : 7*Gfg*8*is*best*9

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), as we create a new list of the same length as the input list using list comprehension

Convert a List to a Comma-Separated String using loop

In Python, we can convert List to Delimeter separated String using a loop. This is one of the ways in which this task can be performed. In this, we run a loop to add a delimiter at the end of each element, after converting each element to a string.

Python3




test_list = [7, "Gfg", 8, "is", "best", 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
delim = "*"
 
res = ''
 
# using loop to add string followed by delim
for ele in test_list:
    res = res + str(ele) + delim
 
print("The resultant string : " + str(res))


Output

The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
The resultant string : 7*Gfg*8*is*best*9

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), because the size of the string res grows linearly with the size of the input list test_list.

Convert a List to a Comma-Separated String using join()

In Python, we can convert List to Delimeter seperated String using join(). This is yet another way in which this task can be performed. In this we perform the task of joining each element by delim using join() and conversion to string is done using str().

Python3




test_list = [7, "Gfg", 8, "is", "best", 9]
 
print("The original list is : " + str(test_list))
 
delim = "*"
 
# using map to convert each element to string
temp = list(map(str, test_list))
 
# join() used to join with delimiter
res = delim.join(temp)
 
print("The resultant string : " + str(res))


Output

The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
The resultant string : 7*Gfg*8*is*best*9

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

Convert a List to a Comma-Separated String using Map()

In Python, we can convert List to Delimeter seperated String using map(). This method we first map each element in Test_list to its string representation using the map() function and the str() method. The resulting strings are then joined together using the join() method and the delimiter.

Python3




test_list = [7, "Gfg", 8, "is", "best", 9]
 
delim = "*"
 
res = delim.join(map(str, test_list))
 
print("The resultant string : " + str(res))


Output

The resultant string : 7*Gfg*8*is*best*9

Time complexity: O(n), where n is the length.
Auxiliary space: O(n) 

Convert a List to a Comma-Separated String using String Formatting and Unpacking

In Python, you can convert a list to a delimiter-separated string using string formatting and unpacking. This approach allows you to control the formatting and customization of the resulting string.

Python3




test_list = [7, "Gfg", 8, "is", "best", 9]
 
print("The original list is : " + str(test_list))
 
# initializing delim
delim = "*"
 
# using string formatting and unpacking to add delim between each element
res = "{}".join(map(str, test_list))
res = res.format(*([delim] * len(test_list)))
 
print("The resultant string : " + str(res))


Output

The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
The resultant string : 7*Gfg*8*is*best*9

Time complexity: O(n)
Auxiliary space: O(n)



Last Updated : 28 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads