Concatenate or print list elements with a trailing comma in Python
join, list, python, string
Solution
String concatenation is the best way:
l = ['1', '2', '3', '4'] # original list
s = ', '.join(l) + ','
but you have other options also:
Mapping to comma-ended strings, then joining:
l = ['1', '2', '3', '4'] # original list
s = ' '.join(map(lambda x: '%s,' % x, l))
Appending empty string to the joined list (don't modify original `l` list!):
l = ['1', '2', '3', '4'] # original list
s = ', '.join(l + ['']).rstrip(' ')
Using string formatting in place of concatenation:
l = ['1', '2', '3', '4'] # original list
s = '%s,' % (', '.join(l))
Problem
I am having a list as : ``` >>> l = ['1', '2', '3', '4'] ``` if I use join statement, ``` >>> s = ', '.join(l) ``` will give me output as : ``` '1, 2, 3, 4' ``` But, what I have to do If I want output as : ``` '1, 2, 3, 4,' ``` (I know that I can use string concat but I want to know some better way) .