Convert List to Comma-Separated String in Python

Convert List to Comma-Separated String in Python

  1. Use the join() Function to Convert a List to a Comma-Separated String in Python
  2. Use the StringIO Module to Convert a List to a Comma-Separated String in Python

We can use a list to store different elements under a common name. A string is a collection of characters.

We will convert a list to a comma-separated string in this tutorial.

Use the join() Function to Convert a List to a Comma-Separated String in Python

The join() function combines the elements of an iterable and returns a string. We need to specify the character that will be used as the separator for the elements in the string.

To create a comma-separated string, we will use the comma as the separator.

See the code below.

lst = ['c','e','a','q']
s = ",".join(lst)
print(s)

Output:

c,e,a,q

The above method is only limited to a list containing strings.

We can use list comprehension and the str() function to work with a list containing integers or other elements. With list comprehension, we can iterate over the elements easily in a single line using the for loop, and convert each element to a string using the str() function.

We implement this in the following code.

lst = [8,9,4,1]
s = ",".join([str(i) for i in lst])
print(s)

Output:

8,9,4,1

We can also eliminate list comprehension by using the map() function. The map() function can be used to convert all elements of the list to a string by applying the str() function to every element.

For example,

lst = [8,9,4,1]
s = ",".join(map(str,lst))
print(s)

Output:

8,9,4,1

Use the StringIO Module to Convert a List to a Comma-Separated String in Python

The StringIO object is similar to a file object but in memory to work with texts. In Python 2, it can be imported directly using the StringIO module. In Python 3, it was stored in the io module.

We can use the csv.writerow() function to write the list as a comma-separated row for a CSV file in the StringIO object. For this, we need to instantiate a csv.writer object first. We can then store the contents of this object in a string using the getvalue() function.

See the following code.

import io
import csv
lst = [8,9,4,1]
s_io = io.StringIO()
writer = csv.writer(s_io)
writer.writerow(lst)
s = s_io.getvalue()
print(s)

Output:

8,9,4,1

We can also use the print() function with the unpack operator. The unpack operator * unpacks all the elements of an iterable, and stores it in the StringIO object using the file parameter in the print() function.

See the code below.

import io
lst = [8,9,4,1]
s_io = io.StringIO()
print(*lst, file=s_io, sep=',', end='')
s = s_io.getvalue()
print(s)

Output:

8,9,4,1
Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn

Related Article - Python List

  • Convert a Dictionary to a List in Python
  • Remove All the Occurrences of an Element From a List in Python
  • Remove Duplicates From List in Python
  • Get the Average of a List in Python
  • What Is the Difference Between List Methods Append and Extend
  • Convert a List to String in Python
  • Related Article - Python String

  • Remove Commas From String in Python
  • Check a String Is Empty in a Pythonic Way
  • Convert a String to Variable Name in Python
  • Remove Whitespace From a String in Python
  • Extract Numbers From a String in Python
  • Convert String to Datetime in Python