Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, print delimited list

Consider this Python code for printing a list of comma separated values

for element in list:     print element + ",", 

What is the preferred method for printing such that a comma does not appear if element is the final element in the list.

ex

a = [1, 2, 3] for element in a   print str(element) +",",  output 1,2,3, desired 1,2,3 
like image 709
Mike Avatar asked Mar 08 '10 03:03

Mike


People also ask

How do I print a separator list in Python?

If you just want to know the best way to print a list in Python, here's the short answer: Pass a list as an input to the print() function in Python. Use the asterisk operator * in front of the list to “unpack” the list into the print function. Use the sep argument to define how to separate two list elements visually.

How do I print a separated list?

When you wish to print the list elements in a single line with the spaces in between, you can make use of the "*" operator for the same. Using this operator, you can print all the elements of the list in a new separate line with spaces in between every element using sep attribute such as sep=”/n” or sep=”,”.

How do you print a list without commas?

use join() function to print array or without square brackets or commas.

How do you make a comma-separated list 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].


1 Answers

>>> ','.join(map(str,a)) '1,2,3' 
like image 97
ghostdog74 Avatar answered Sep 22 '22 16:09

ghostdog74