Convert List to Comma Separated String in Python

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.

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.

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.

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.

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *