Print Delimited list

0 votes
Here is a 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.

For example

a = [1, 2, 3]
for element in a
  print str(element) +",",

output
1,2,3,
desired
1,2,3
Feb 28 in Python by ana1504.k
• 7,890 points
103 views

1 answer to this question.

0 votes
You can try the following and see if it works :

>>> ','.join(map(str,a))
'1,2,3'
answered Feb 28 by SDeb
• 13,190 points

Related Questions In Python

+2 votes
11 answers
0 votes
1 answer

How to Print a List in Python

ou are using Python 2.x syntax with ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,720 points
102 views
0 votes
1 answer

print(instance(0).id) TypeError: 'list' object is not callable

Hey @Suraj, For me its working fine. ...READ MORE

answered Jan 18 in Python by Priyaj
• 56,940 points
267 views
0 votes
1 answer

Print a list in both ascending and descending order

You can use listName.sort(reverse=True). This will sort the ...READ MORE

answered Feb 6 in Python by Omkar
• 67,660 points
44 views
0 votes
1 answer

How can I print variable and string on same line in Python?

Use , to separate strings and variables while printing: print ...READ MORE

answered Sep 17, 2018 in Python by Priyaj
• 56,940 points
76 views
+1 vote
1 answer

Python: Print variable and string in same line

For a better understanding you can refer ...READ MORE

answered Oct 30, 2018 in Python by Priyaj
• 56,940 points
113 views
0 votes
1 answer

How do I print this list vertically?

You can try the following code: myList = ...READ MORE

answered Apr 5 in Python by SDeb
• 13,190 points
55 views
0 votes
1 answer

Print C format in Python

For printf- style formatting and special case ...READ MORE

answered Sep 18, 2018 in Python by SDeb
• 13,190 points
46 views