Kodeclik Blog
How to convert a Python list to a string
In your Python journey, you will sometimes find a need to convert a Python list into a string. In other words, you will desire to take the elements of the list and place them as substrings of a larger string. There are three ways to do so!
Method 1: Use the join() method
The join() method is a simple and elegant way to concatenate elements from a list into a string with a specified separator. In our case, the separator will be a comma. Let's see how this method works:
my_list = ['Hong Kong','Milan','New York','Beijing']
my_string = ', '.join(my_list)
print(my_string)
The output is:
Hong Kong, Milan, New York, Beijing
By using the join() method, notice that we did not need any loops and have efficiently converted the list to a comma-separated string.
Method 2: Use list comprehensions
List comprehension is a powerful feature in Python that allows us to create a new list based on an existing list or iterable. By leveraging list comprehension and the str() function, we can convert each element of the list to a string and then join them together with commas.
my_list = ['Hong Kong','Milan','New York','Beijing']
my_string = ', '.join(str(x) for x in my_list)
print(my_string)
The output is again:
Hong Kong, Milan, New York, Beijing
This method is particularly useful when the list contains elements of different data types, as it converts them all to strings before concatenation.
Method 3: Use a combination of map() and str()
A final approach to convert a Python list to a comma-separated string is by using the map() function in combination with the str() function and the join() method. The map() function applies the str() function to each element of the list, converting them to strings.
my_list = ['Hong Kong','Milan','New York','Beijing']
my_string = ', '.join(map(str, my_list))
print(my_string)
Note that the above code is a small variant of the one used in Method 2. The output is again:
Hong Kong, Milan, New York, Beijing
By using the map() function, we avoid explicit iteration and achieve a more concise and readable solution.
In this blog post, we explored three efficient ways to convert a Python list to a string with commas. Whether you prefer the simplicity of join(), the versatility of list comprehension, or the combination of map() and join(), these methods offer straightforward solutions to this common task. Which one is your favorite?
If you liked this blogpost, learn how to do the reverse, i.e., how to convert a comma separated string into a list! Also see how to create a Python list of the alphabetic characters.
If you liked this blogpost, learn about Python string truncation which shortens strings to desired limits.
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.