Select Page

How to Convert a List to a String in Python

by | Programming, Python, Tips

If you need to convert a list to a string in Python, there are several straightforward ways to do so.

This tutorial will go through how to convert a list to a string with the different approaches with code examples.


Convert a List to a String Using Iteration

Iteration refers to using a for loop to go through each element in a list perform some operation. We want to add each element in a list to an empty string in our case. Let’s look at an example function to perform this task:

def list_to_string(s):

    my_string = ""

    for i in s:
        my_string = my_string + i + " "
    
    return my_string

In the above code, the function accepts a list as an argument. The function defines an empty string and a for loop to add each element in the list to the empty string. In addition to the element in the list, the function adds white space. Once all elements are in the string, the function returns it. Let’s define a list of strings and pass it to the list_to_string function as we call it.

my_list = ["Python", "is", "really", "fun", "to", "learn"]

print(list_to_string(my_list))

Let’s run the code to get the output:

Python is really fun to learn 

Convert a List to a String Using join()

The join() method takes all items in the list and joins them into one string. The join() method needs a string as a separator. In this example, we will use white space as the separator. Let’s look at an example function to convert a list to a string:

def list_to_string(s):

    my_string = " ".join(s)

    return my_string

Let’s pass the list to the function call:

my_list = ["Python", "is", "really", "fun", "to", "learn"]
print(list_to_string(my_list))

The code returns:

Python is really fun to learn 

Convert a List to a String Using map()

There can be cases where you want to convert a list that contains both strings and integers to a string. You cannot use join() or iteration to a list of integer and string because they use concatenation, and concatenation is only permissible for strings with other strings. We can use the map() function to map the str() method to the elements in a list. Let’s look at an example:

my_list = [2, "times", 8, "is", 16]

my_str = ' '.join(map(str, my_list))

print(my_str)

In the above code, the map() method converts all the elements in the list to string. We can then use the join() method to convert the list of strings to a string, using the same white-space separator as in the previous example. Let’s run the code to get the result:

2 times 8 is 16

Convert a List to a String Using List Comprehension

We can convert a list to a string using list comprehension. This method is helpful for converting a list of integers and strings to a string. Let’s look at an example:

my_list = [2, "times", 8, "is", 16]

my_str = ' '.join([str(item) for item in my_list])

print(my_str)

In the above code, the list comprehension creates a new list by converting the elements in the original list to strings. We pass the new list to the join() method, which will convert it to a string. Let’s run the code to get the output:

2 times 8 is 16

Summary

Congratulations on reading to the end of this tutorial! The most efficient way to convert a list of strings to a string is to use the join() method. If you want to convert a list of strings and integers to a string, the most efficient way is to use map() on the list and pass the output to join().

For further reading on manipulation of lists, go to the articles:

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!