Reputation: 19164
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)
.
Upvotes: 11
Views: 23301
Reputation: 11
I don't know if this is what you are looking for? It works though.
l = [1,2,3,4]
for num in l: print(str(num), end=", ")
Upvotes: 0
Reputation: 133584
This IDLE session is a good example of how to pull the list elements into a string with a trailing comma:
>>> l = ['1', '2', '3', '4']
>>> ' '.join(x+',' for x in l)
'1, 2, 3, 4,'
Upvotes: 0
Reputation: 137390
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))
Upvotes: 14
Reputation: 32105
If you are in Python 3, you could leverage the print built-in function:
print(*l, sep=', ', end=',')
*l
unpacks the list of elements to pass them as individual arguments to printsep
is an optional argument that is set to in between elements printed from the elements, here I set it to ', '
with a space as you requireend
is an optional argument that will be pushed at the and of the resulting printed string. I set it to ','
without space to match your needYou can use it starting Python 2.6 by importing the print function
from __future__ import print_function
However going this way has several caveats:
file
optional argument into a file__future__
import can break you code compatibility so you would need to isolate your code in a separate module if the rest of your code is not compatible.Long story short, either this method or the other proposed answers are a lot of efforts to try to avoid just adding a +','
at the end of the join
resulting string
Upvotes: 7
Reputation: 23871
If you don't want concatenation, things could be hacky...
>>> l = [1, 2, 3, 4]
>>> repr(l+[0])[1:-3] # or str
'1, 2, 3, 4,'
Upvotes: 1
Reputation: 500495
Firstly, ','.join(l)
won't work at all since it requires the elements to be strings, which they are not.
You can fix that and add the trailing comma like so:
In [4]: ', '.join(map(str, l)) + ','
Out[4]: '1, 2, 3, 4,'
I think this is by far the cleanest way to do it.
Upvotes: 1
Reputation: 26351
For str.join()
to work, the elements contained in the iterable (i.e. a list here), must be strings themselves. If you want a trailing comma, just add an empty string to the end of your list.
Edit: To flesh it out a bit:
l = map(str, [1,2,3,4])
l.append('')
s = ','.join(l)
Upvotes: 5
Reputation: 399881
To include the spaces you've shown, you need to have a space in the separator string:
s = ", ".join(l) + ","
Or make it DRYer:
sep = ", "
s = (sep.join(l) + sep).rstrip()
In the latter, rstrip()
is just used to remove the final trailing space (part of sep
) since that's not in your desired output. It's not the cheapest way of doing this, but it's pretty clear.
Upvotes: 1