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 badges503 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
20.1k3 gold badges23 silver badges42 bronze badges
answered Jan 13, 2009 at 11:46
Tom DunhamTom Dunham
5,6692 gold badges30 silver badges26 bronze badges
Convert list of integers to comma-separated string in Python #
To convert a list of integers to a comma-separated string:
- Use a generator expression to iterate over the list.
- Use the
str[]
class to convert each integer to a string. - Use the
str.join[]
method to join the values into a string with a comma separator.
Copied!
list_of_integers = [1, 3, 5, 7] my_str = ','.join[str[item] for item in list_of_integers] print[my_str] # 👉️ 1,3,5,7
The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
Note that the method raises a TypeError
if there are any non-string values in the
iterable.
If your list contains numbers or other types, you have to convert all of the values to strings before calling join[]
.
We used a generator expression to iterate over the list and used the str[]
class to convert each integer to a string.
Copied!
list_of_integers = [1, 3, 5, 7] my_str = ','.join[str[item] for item in list_of_integers] print[my_str] # 👉️ 1,3,5,7
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.
The string the join[]
method is called
on is used as the separator between the elements.
If you don't need a separator and just want to join the list's elements into a string, call the join[]
method on an empty string.
Copied!
list_of_integers = [1, 3, 5, 7] my_str = ''.join[str[item] for item in list_of_integers] print[my_str] # 👉️ '1357'
If you need to convert the list of integers to a space-separated string, call the join[]
method on a string containing a space.
Copied!
list_of_integers = [1, 3, 5, 7] my_str = ' '.join[str[item] for item in list_of_integers] print[my_str] # 👉️ 1 3 5 7
Alternatively, you can use the map[]
function to convert the values in the list to strings before calling join[]
.
Copied!
list_of_integers = [1, 3, 5, 7] my_str = ' '.join[map[str, list_of_integers]] print[my_str] # 👉️ 1 3 5 7
The map[] function takes a function and an iterable as arguments and calls the function with each item of the iterable.
The str[]
class gets called with each number in the list and converts the values to strings.