Convert List to String in Python

To convert a list to a string in python use the join() method, this method is used to join all the elements from the list with the specified separator and return a string. Besides this, there are other approaches as well to convert a list to a string. In this article. I will cover all these that convert a list to a string. Note that list can store elements of multiple data types and a string is a sequence of characters.

1. Quick Examples of Converting List to String

Following are quick examples of how to convert a list to a string in python.


# Quick Examples

# Create a List with 5 elements
myList=['Welcome','to','spark','by','examples']

# Use join().
myString = " ".join(myList)

# Use join() with map().
myString " ".join(map(str, myList))

# Use List Comprehension with join().
myString = " ".join([i for i in myList])

# Use for loop to convert list to string
myString=""
for x in myList:
    myString = myString + " " + x

# Use str.format()
myString = "{} {} {} {} {}".format(*myList)

2. Python Convert List to String using join()

The join() in python is used to convert the list to a string. This method takes the list of strings as an argument, joins with the specified separator, and returns it as a string. Actually, join can take any iterable as an argument and returns a combined string with a space separator, you can also use this method to join the elements with any other string as a separator.

Note that using join is often faster and more memory-efficient than concatenating the strings using the + operator, especially when the number of elements in the list is large.

2.1 join() Syntax

Following is the syntax of the join(). Note that the values in the iterable should be of string data type.


# Syntax of join()
" ".join(list1)

2.2 Example

Let’s create a list named ‘myList’ and convert this list to a string using join(). In the below example, I used the space separator that separates each element in the string.

Make sue you have a list of elements with string type. In case you have integers you will get a TypeError.


# Use join() to convert list to string
myList=['Welcome','to','spark','by','examples']
print(myList)

# Use join().
myString = " ".join(myList)
print(myString)

Yields below output.

python convert list to string

Let’s try the same example with a comma separator.


# Use join() with comma separator
myString = ",".join(myList)

# Output:
# Welcome,to,spark,by,examples

3. Convert List to String using map()

Here, we will utilize the join() method and pass the map() function as a parameter to join() method. map() will convert the list of elements to a string by taking str as the first parameter and the list name as the second parameter. str refers to the string data type.

You can also use this approach if you have a list of elements with an integer type. The map() methods execute a specific function for all elements in an iterable. Here, we used it to convert each value in the list into a string.

3.1 map() Syntax

Following is the syntax of the join() with map()


# Syntax
# str refers to the string datatype and 
#list1 is the name of our list.
" ".join(map(str, list1))

3.2 Example

Let’s use the join() with map() to convert the list to a string.


myList=['Welcome','to','spark','by','examples']
myAnohterList = [10,20,30,40]
print(myList)
print(myAnohterList)

# Use join() with map().
print(" ".join(map(str, myList)))
print(" ".join(map(str, myAnohterList)))

Yields below output.

python convert list to string

4. List to String using List Comprehension with join()

Here, we will utilize the join() method and pass the list comprehension as a parameter to it. Inside the list comprehension, we will iterate all the elements in a list and join them. By using this you can filter the string values from the list that you don’t want to convert.

4.1 List Comprehension with join() Syntax


" ".join([iterator for iterator  in list1])

4.2 Example


myList=['Welcome','to','spark','by','examples']
print(myList)

# Use List Cpmprehension with join().
print(" ".join([i for i in myList]))

# Output:
# ['Welcome','to','spark','by','examples']
# Welcome to spark by examples

5. Convert List to String using for loop in Python

Alternatively, you can also convert the list to a string by using the for loop to iterate the list. So we will iterate all the elements in the list and concatenate them to the string using the + operator. By using this also you can use any separator when joining strings.

5.1 For loop Syntax


# Use for loop to convert list to string, Here, list1 is the input list and 
# iterator is used to iterate elements in the list.
for iterator in list1:
    print(iterator ,end=" ")

5.2 Example

Let’s use the for loop to convert the list to a string. Note that the join method can be very useful when you want to convert a python list to string. It is often faster and more memory-efficient than concatenating the strings using the + operator, especially when the number of strings is large.


myList=['Welcome','to','spark','by','examples']
print(myList)

# Use for loop to convert list to string
myString = ""
for x in myList:
    myString = myString+" "+x
print(myString)

# Output:
# ['Welcome','to','spark','by','examples']
# Welcome to spark by examples

6. Python Convert List to String using format()

The format() method in python can also be used to convert a list to a string. Here, we can pass our list inside this method by unpacking it. Unpacking is done by specifying the unpack operator – *.

It is important to specify the {} for each string present in the list. If the total number of elements in the list is 5, then you need to specify 5 {}’s. Note that this approach can be used only when you have a few elements in a list, not suggestable when you have a large list.

6.1 format() Syntax


#Here, list1 is the input list
"{} ...".format(*list1)

6.2 Example


myList=['Welcome','to','spark','by','examples']
print(myList)

# Use str.format()
print("{} {} {} {} {}".format(*myList))

# Output:
# ['Welcome','to','spark','by','examples']
# Welcome to spark by examples

7. Conclusion

So we have seen five different ways to convert the given list of elements to a string in Python. Majorly we have used the join() method and for loop to convert a list of elements to a string. Remember you need to specify {} while using the format() method and a total number of {} should be equal to the total number of elements in the list. If you miss, then only some elements return as a string.

Leave a Reply

You are currently viewing Convert List to String in Python