Use .join()
Method
To convert a list to a comma separated string in Python, use the .join()
method. join()
method takes all elements in an iterable and joins them into one string with delimiter as separator.
1 2 3 4 5 |
list_of_strings = ['string_one', 'string_two', 'string_three'] comma_separated_strings = ','.join(list_of_strings) print(comma_separated_strings) |
1 2 3 |
string_one,string_two,string_three |
First, we created a list having string-type elements and named that list list_of_strings
. Next, we used the .join() method to join all list elements into strings using a comma as a separator. This method took an iterable as a parameter, which is list_of_strings
, for this code example.
Here, iterable means the objects which can return their members/elements one at a time. We used a comma as a separator, but you can use a separator of your choice; it can be #
, whitespace
, etc.
Suppose the list contains non-string elements, such as int
, bool
, None
, etc. Now, using the .join()
method only will result in an error saying TypeError: sequence item 1: expected str instance, int found
.
To handle this scenario, we can use the .join()
with the map()
method, as demonstrated in the following section.
Use .join()
with map()
Method
To convert a list having string and integer type elements to a comma separated strings in Python:
- Use the
map()
method to transform the data type of all list elements to string. - Use the
.join()
method to join all the list elements (now of string data type) and separate them with a comma.
1 2 3 4 5 |
list_of_alphanumeric = ['string_one', 10, 'string_two', 'string_three'] comma_separated_strings = ','.join(map(str, list_of_alphanumeric)) print(comma_separated_strings) |
1 2 3 |
string_one,10,string_two,string_three |
In the previous section, we learned about the .join()
method while converting a list_of_strings
to comma-separated strings. Here, we used the map()
method to map every list element to a string type value.
Once the mapping is performed on all list elements, we pass this map
type object to the .join()
method to join them separated by a comma.
Here, map(str, list_of_alphanumeric)
maps the type of every element of list_of_alphanumericlist where every element is passed to str()
function as its parameter.
Further reading:
Use .join()
with List Comprehension
There is an alternative of using .join()
with map()
method, which is using .join()
method with list comprehension as follows:
- Use a list comprehension to iterate over each element of the list. In each iteration, pass the list element to
str()
for type casting. - Once all elements are cast to string data type, pass the converted list to the
.join()
method to join all the list elements separated by a comma.
1 2 3 4 5 |
list_of_alphanumeric = ['string_one', 10, 'string_two', 'string_three'] comman_separated_strings = ','.join([str(x) for x in list_of_alphanumeric]) print(comman_separated_strings) |
1 2 3 |
string_one,10,string_two,string_three |
After creating a list, we used list comprehension, which looped over the elements of the list_of_alphanumeric
. Finally, we passed the current element to the str()
method for type casting in each iteration. Here, type casting means changing the element’s datatype from one another.
Next, we passed the transformed list having all string-type elements to the .join()
method, which joins all the elements by separating them using the given separator. We used ,
as the separator; you can use whatever you want.
That’s all about how to convert list to comma separated Strings in Python.