Python list of integers to comma separated string
I'm new to python, and have a list of longs which I want to join together into a comma separated string. Show In PHP I'd do something like this:
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:
answered Jan 13, 2009 at 11:28 4 You can use map to transform a list, then join them up.
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:
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?)
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:
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:
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 If your list contains numbers or other types, you have to convert all of the values to strings before calling We used a generator expression to iterate over the list and used the
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition. The string the If you don't need a separator and just want to join the list's elements into a string, call the
If you need to convert the list of integers to a space-separated string, call the
Alternatively, you can use the
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable. The How do you separate a list with commas?Text to Columns. Highlight the column that contains your list.. Go to Data > Text to Columns.. Choose Delimited. Click Next.. Choose Comma. Click Next.. Choose General or Text, whichever you prefer.. Leave Destination as is, or choose another column. Click Finish.. How do you print a list of numbers separated by commas in Python?We use another way to demonstrate the working of a Python list to a comma-separated string. At first, we initialize a list and use the print function to print original values in the list. Then we use a loop to add a comma at the end of each value, after altering each value to a string.
How do you convert a list to a commaApproach: This can be achieved with the help of join() method of String as follows. Get the List of String. Form a comma separated String from the List of String using join() method by passing comma ', ' and the list as parameters. Print the String.
How do you convert a list of integers to a commaUse the join() Function to Convert a List to a Comma-Separated String in Python. The join() function combines the elements of an iterable and returns a string. We need to specify the character that will be used as the separator for the elements in the string.
|