Python join list of ints with comma

I'm new to python, and have a list of longs which I want to join together into a comma separated string.

In PHP I'd do something like this:

$output = implode[",", $array]

In Python, I'm not sure how to do this. I've tried using join, but this doesn't work since the elements are the wrong type [i.e., not strings]. Do I need to create a copy of the list and convert each element in the copy from a long into a string? Or is there a simpler way to do it?

asked Jan 13, 2009 at 11:24

3

You have to convert the ints to strings and then you can join them:

','.join[[str[i] for i in list_of_ints]]

answered Jan 13, 2009 at 11:28

4

You can use map to transform a list, then join them up.

",".join[ map[ str, list_of_things ] ]

BTW, this works for any objects [not just longs].

answered Jan 13, 2009 at 11:30

S.LottS.Lott

376k78 gold badges502 silver badges771 bronze badges

2

You can omit the square brackets from heikogerlach's answer since Python 2.5, I think:

','.join[str[i] for i in list_of_ints]

This is extremely similar, but instead of building a [potentially large] temporary list of all the strings, it will generate them one at a time, as needed by the join function.

2

and yet another version more [pretty cool, eh?]

str[list_of_numbers][1:-1]

answered Jul 22, 2009 at 7:36

fortranfortran

71.8k25 gold badges133 silver badges173 bronze badges

0

Just for the sake of it, you can also use string formatting:

",".join["{0}".format[i] for i in list_of_things]

Hubert Kario

20k3 gold badges23 silver badges42 bronze badges

answered Jan 13, 2009 at 11:46

Tom DunhamTom Dunham

5,6692 gold badges29 silver badges26 bronze badges

Not the answer you're looking for? Browse other questions tagged python or ask your own question.

  1. HowTo
  2. Python How-To's
  3. Convert List to Comma-Separated String in Python

Created: January-03, 2022

  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

Write for us

DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

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

    Related Article - Python String

  • 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
  • How do you convert a list of integers to a comma separated string?

    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.

    How do you put a comma in a list in Python?

    Use str..
    a_list = ["a", "b", "c"].
    joined_string = ",". join[a_list] Concatenate elements of `a_list` delimited by `","`.
    print[joined_string].

    How do you join a string with a comma in Python?

    Python concatenate strings with a separator We can also concatenate the strings by using the ',' separator in between the two strings, and the “+ ” operator is used to concatenate the string with a comma as a separator. To get the output, I have used print[str].

    How do you split a list by comma in Python?

    Use the str. split[] method to split a string by comma, e.g. my_list = my_str. split[','] .

    Chủ Đề