Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a list of longs into a comma separated string in python [duplicate]

Tags:

python

People also ask

How do I convert a list to a comma-separated string in Python?

How to Convert a Python List into a Comma-Separated String? You can use the . join string method to convert a list into a string. So again, the syntax is [seperator].

How do you convert a list to a comma-separated string?

The standard solution to convert a List<string> to a comma-separated string in C# is using the string. Join() method. It concatenates members of the specified collection using the specified delimiter between each item.

How do you print a list of values separated by a comma in Python?

for i, x in enumerate(a): if i: print ',' + str(x), else: print str(x), this is a first-time switch (works for any iterable a, whether a list or otherwise) so it places the comma before each item but the first.

How do you convert a long string to a list in Python?

Converting string to list in Python: Data type conversion or type casting in Python is a very common practice. However, converting string to list in Python is not as simple as converting an int to string or vice versa. Strings can be converted to lists using list() .


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

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

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


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.


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

str(list_of_numbers)[1:-1]