5 Ways to Convert List to String Python
In this article, you will learn 5 ways to convert list to string in Python.
We need to convert a list to a string while programming for many reasons. For example, we need to convert a list to a string to store it in a database or to send it to the server, we need to store the list in a file and then read the list from the file, or we need to display the list, etc.
- Quick Solution
- Methods to convert list to string
- Conclusion
Table Of Contents
Quick Solution
The easiest and quickest way to convert a list to a string is to use the join() method.
Call the join() method on a separator and pass the list as an argument. The method will join the elements of the list with the separator and return the string.
Here is an example to convert a list to a string.
# convert list to string python
items = ['Simple', 'Python', 'Code']
# join the list with space
str = ' '.join(items)
print(str)
print(type(str))
Simple Python Code <class 'str'>
Look at the code above, the separator is a space that's why elements of the list are joined together with a space in-between.
Methods to convert list to string
Let's now look at 5 different ways to convert a list to a string in Python with examples.
1. Using join() method
The join() method is a string method in Python that joins the elements of the given list with a separator into a string.
After creating the string it returns it.
Syntax:
string.join(sequence)
Here 'string' is the string that we want to join the elements of the list with. 'sequence' is the list that we want to join.
# join elements together
items = ['a', 'b', 'c', 'd', 'e']
str = ''.join(items)
print(str)
abcde
In the above example separator is an empty string. So elements of the list are joined together with nothing in-between.
Here is another example that joins the elements of the list with different separators like space, comma, dash, etc.
items = ['a', 'b', 'c', 'd', 'e']
str = ' '.join(items)
print(str)
str = ','.join(items)
print(str)
str = '-'.join(items)
print(str)
a b c d e a,b,c,d,e a-b-c-d-e
2. Python list to string using loop
Another way to convert a list to a string is to use a loop. We will use for loop here.
To convert a list to a string, loop through the list items of the list and concatenate them with a separator or without a separator as you wish.
Here is an example to convert a list to a string using for loop.
# convert list to string using for loop
items = ['Simple', 'Python', 'Code']
# initialize an empty string
string = ""
# loop through the list
for item in items:
string = string + item
print(string)
SimplePythonCode
In the above example, we are not using a separator. So elements of the list are joined together without any separator.
To add a separator just as we did in the join() method add a separator string at the end of the element.
items = ['Simple', 'Python', 'Code']
# initialize an empty string
string = ""
sep = " "
# loop through the list
for item in items:
string = string + item + sep
print(string)
# comma separator
sep = ","
for item in items:
string = string + item + sep
print(string)
Simple Python Code Simple,Python,Code
3. Using List comprehension
Another way to convert a list to a string is to use list comprehension.
List comprehension is used when you have a number as an element in the list. The join() method shows a TypeError error because we are trying to join a string with a number.
So first we need to convert the number to a string and then apply the join() method.
# applying join() on number
items = [1, 2, 3, 4, 5]
print(' '.join(items)) ❌# TypeError
TypeError: sequence item 0: expected str instance, int found
First, convert the number to a string using the str() method.
items = [1, 2, 3, 4, 5]
# using list comprehension
print(','.join([str(item) for item in items]))
Output
1,2,3,4,5
In the above example, list comprehension converts the numbers in a list to strings, and then the join() method joins the elements of the list to a string.
This can also be used when you have heterogeneous data like a list of strings and numbers mixed in the list.
items = ['a', 1, 2,'b', 3, 4, 'c', 5]
# convert heterogeneous list to string
print(','.join([str(item) for item in items]))
a,1,2,b,3,4,c,5
4. Using map() method
The map() method can be used in similar cases as above:
- If you have a list of numbers and you want to convert them to strings.
- If you have a heterogeneous list like a list of strings and numbers mixed in the list and you want to convert them to strings.
The map() function takes two arguments: a function and a list.
The Python function is applied to each element of the list and the result is returned as a list.
Here we will use str() as a function to convert the numbers in the list to strings.
items = [1, 2, 3, 4, 5]
# using map()
print(','.join(map(str, items)))
1,2,3,4,5
Here is an example of the heterogeneous list.
items = ['a', 1, 2,'b', 3, 4, 'c', 5]
# convert heterogeneous list to string
print(','.join(map(str, items)))
1,2,3,4,5
5. Using reduce() method
The reduce() method is used to reduce a list to a single value.
To use the reduce() function you have to import the functools module.
The reduce() function takes two arguments: a function and a list.
The function is applied to each element of the list and the result is returned as a single value.
# import functools
from functools import reduce
items = ['Python', 'Is', 'Cool']
# using reduce()
print(reduce(lambda a, b: a + b, items))
PythonIsCool
In the above example, the reduce() function accepts a lambda function as an argument that concatenates the elements of the list.
Note: Apply the reduce() function only when the list contains either only strings or only numbers. Otherwise, it will throw a TypeError.
Using a separator to join the elements using reduce() function.
# import functools
from functools import reduce
items = ['Python', 'Is', 'Cool']
# using reduce()
# with separator
print(reduce(lambda a, b: a + ' ' + b, items))
Python Is Cool
Conclusion
Summarising convert list to string python article, we have learned how to convert a list to a string in Python using 5 different methods.
The join() method is most commonly used to convert a list to a string. You can control the separator using it.
The other 4 methods have their own importance in different situations.