34

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?

Ben
  • 66,838
  • 37
  • 84
  • 108
  • Not a duplicate; the solution suggested in [#44778](https://stackoverflow.com/q/44778/27358) (`",".join(list)`) only works for a list of strings. With a list of numbers they give you **TypeError: sequence item 0: expected string, int found**. (Yes, the `str()` business is in there, but it's buried.) – David Moles Apr 18 '18 at 17:50
  • I'm voting to keep this closed because all the answers on the duplicate involve converting the long to a string, and the title doesn't lead people to find out how to join a list of longs (or other not-strings) at all. But I suspect that StackOverflow may be asking us this as a test, given that this question is now nearly 10 years old... – Lunivore Apr 18 '18 at 23:05
  • @Ben Would you mind if we generalize this question more to just "integers"? – rrauenza Jan 09 '20 at 23:30

5 Answers5

71

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

','.join([str(i) for i in list_of_ints])
  • 7
    Non-beginner note: In Python 2.4+ you don't need the `[ ]` around the generator expression -- it'll be more efficient if you leave it off. – cdleary Jan 13 '09 at 20:45
  • @cdleary: I realize this is 4 years late, but… it will usually be a bit _less_ efficient if you use a genexp instead of a listcomp here. At least some implementations of `str.join` iterate the sequence twice, meaning they have to turn it your generator into a list anyway. In particular, CPython 2.4-[3.3](http://hg.python.org/cpython/file/3.3/Objects/unicodeobject.c#l9473) uses the `PySequence_FAST` protocol, and PyPy [2.0.0](https://bitbucket.org/pypy/pypy/src/release-2.0.0/pypy/objspace/std/stringobject.py#cl-378) creates a `listview`. – abarnert May 12 '13 at 10:01
  • 1
    That being said, if efficiency _isn't_ an issue (and it usually won't be), I'd probably use the genexp anyway. There's no _conceptual_ reason to build an explicit list here when all you really care about is building an iterable. – abarnert May 12 '13 at 10:05
  • Why didn't Python join method do the str conversion itself? The code would have then simply read `",".join(list_of_ints)` which is much more cleaner and easier to understand. – arun Oct 31 '13 at 23:09
21

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).

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • as for the Weeble comment, maybe it's better to use itertools.imap() if list_of_things is very big –  Jan 13 '09 at 12:11
  • 1
    Few things are big enough or time-critical enough to justify itertools over built-in map. However, if benchmarking reveals that this is the bottleneck, you've got a way to speed things up. – S.Lott Jan 13 '09 at 12:29
11

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.

Weeble
  • 17,058
  • 3
  • 60
  • 75
  • Python 2.4 added Generator Expressions, bounded by parens, generating values one at a time, unlike the square-bracketed list comprehensions which generate the entire list. Dropping the square brackets becomes a Generator Expression due to a shortcut for single-param function calls. – Andy Dent Jan 13 '09 at 12:20
  • … except that the `join` function will build the list anyway, so you don't gain anything (and it may be a bit slower to build a list from a genexp than to just use a listcomp). See my comment to unbeknown's answers for details. – abarnert May 12 '13 at 10:03
7

and yet another version more (pretty cool, eh?)

str(list_of_numbers)[1:-1]
fortran
  • 74,053
  • 25
  • 135
  • 175
3

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

",".join("{0}".format(i) for i in list_of_things)
Hubert Kario
  • 21,314
  • 3
  • 24
  • 44
Tom Dunham
  • 5,779
  • 2
  • 30
  • 27