Convert List to Delimiter Separated String - Python
Last Updated :
08 Feb, 2025
The task of converting a list to a delimiter-separated string in Python involves iterating through the list and joining its elements using a specified delimiter. For example, given a list a = [7, "Gfg", 8, "is", "best", 9] and a delimiter "*", the goal is to produce a single string where each element is converted to a string and separated by "*", resulting in "7*Gfg*8*is*best*9".
Using str.join()
str.join() is the most efficient way to convert a list into a delimiter-separated string. It works by first converting each element into a string and then joining them using the specified delimiter. This method is highly optimized for performance and is the preferred choice in most cases.
Python
a = [7, "Gfg", 8, "is", "best", 9]
delim = "*"
res = delim.join(map(str, a))
print(res)
Explanation: map(str, a) converts each element of a into a string since integers cannot be directly joined and delim.join(...) joins all converted string elements using * as a separator.
Using list comprehension
List comprehension allows transforming each element into a string before joining them, making it useful when additional modifications are needed. While slightly less efficient than map(str, a), it remains a clean and readable alternative to str.join().
Python
a = [7, "Gfg", 8, "is", "best", 9]
res = "*".join([str(ele) for ele in a])
print(res)
Explanation: [str(ele) for ele in a] converts each element of a into a string, and "*".join(...) joins all converted elements using * as a separator, creating a single formatted string.
Using reduce()
reduce() from the functools module applies a binary function iteratively to combine all elements into a single string. Though functional in nature, it is less efficient than str.join() due to repeated string creation.
Python
from functools import reduce
a = [7, "Gfg", 8, "is", "best", 9]
res = reduce(lambda x, y: str(x) + "*" + str(y), a)
print(res)
Explanation: reduce() applies the lambda function cumulatively to the list elements, where lambda x, y: str(x) + "*" + str(y) converts x and y to strings, concatenates them with * as a separator and continues this process until all elements are merged into a single string.
Using for loop
A traditional for loop can manually concatenate elements into a string. Although simple and intuitive, this approach is inefficient due to repeated immutable string operations, making it slower for large lists.
Python
a = [7, "Gfg", 8, "is", "best", 9]
res = ""
for ele in a:
res += str(ele) + "*"
res = res[:-1]
print(res)
Explanation: for loop converts each element to a string, appends it to res with * and res[:-1] removes the trailing * for proper formatting.
Similar Reads
Python - Convert delimiter separated Mixed String to valid List Given a string with elements and delimiters, split elements on delimiter to extract with elements ( including containers). Input : test_str = "6*2*9*[3, 5, 6]*(7, 8)*8*4*10", delim = "*" Output : [6, 2, 9, [3, 5, 6], (7, 8), 8, 4, 10] Explanation : Containers and elements separated using *. Input :
10 min read
Python - Convert Delimiter separated list to Number Given a String with delimiter separated numbers, concatenate to form integer after removing delimiter. Input : test_str = "1@6@7@8", delim = '@' Output : 1678 Explanation : Joined elements after removing delim "@"Input : test_str = "1!6!7!8", delim = '!' Output : 1678 Explanation : Joined elements a
6 min read
How To Convert Comma-Delimited String to a List In Python? In Python, converting a comma-separated string to a list can be done by using various methods. In this article, we will check various methods to convert a comma-delimited string to a list in Python.Using str.split()The most straightforward and efficient way to convert a comma-delimited string to a l
1 min read
Python - Sort words separated by Delimiter Given string of words separated by some delimiter. The task is to sort all the words given in the string Input : test_str = 'gfg:is:best:for:geeks', delim = "*" Output : best*for*geeks*gfg*is Explanation : Words sorted after separated by delim. Input : test_str = 'gfg:is:best', delim = "*" Output :
6 min read
Python | Delimited String List to String Matrix Sometimes, while working with Python strings, we can have problem in which we need to convert String list which have strings that are joined by deliminator to String Matrix by separation by deliminator. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop + split() T
5 min read