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, 2019 in Python by ana1504.k
• 7,910 points
889 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, 2019 by SDeb
• 13,300 points

Related Questions In Python

+1 vote
12 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
1,107 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, 2019 in Python by Priyaj
• 58,020 points
3,006 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 7, 2019 in Python by Omkar
• 69,220 points
1,576 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
• 58,020 points
3,541 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
• 58,020 points
1,562 views
0 votes
2 answers
0 votes
1 answer

How do I print this list vertically?

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

answered Apr 5, 2019 in Python by SDeb
• 13,300 points
5,586 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,300 points
1,028 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP