How to convert a long list to a string separated by commas in python


This question already has an answer here:

  • How would you make a comma-separated string from a list? 12 answers

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?

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

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