how to join array of integers?

John Machin sjmachin at lexicon.net
Sat Sep 15 08:51:20 EDT 2007


On Sep 15, 10:36 pm, Summercool <Summercooln... at gmail.com> wrote:
> i think in Ruby, if you have an array (or list) of integers
>
> foo = [1, 2, 3]
>
> you can use foo.join(",") to join them into a string "1,2,3"
>
> in Python... is the method to use  ",".join() ?  but then it must take
> a list of strings... not integers...
>
> any fast method?

>>> foo = [1,2,3]
>>> ",".join(str(x) for x in foo)
'1,2,3'
>>> ",".join(map(str, foo))
'1,2,3'
>>>

If you are going to write several such results to a file, consider
using the csv module.

HTH,
John




More information about the Python-list mailing list