List needs to be converted to a string and perform a string operations,
Is there a effective way to convert both the operations into 1,
lista = ['col-1,','col-2,','col-3,']
stra = ' '.join(lista)
stra = stra.rstrip(',')
How about ', '.join(['col-1', 'col-2', 'col-3'])? Then you don't need rstrip at all.
join only works for single characters, and munged the data to add commas. Or maybe you're right and the data is provided in that form. We'll see :)Since the join() method returns a string you can chain methods:
stra = ' '.join(lista).rstrip(',') # prints col-1, col-2, col-3
Or, if you want to strip the commas before joining the strings (since you have the comma at the end of every string I'm assuming you want to):
stra = ' '.join(map(lambda s: s.rstrip(','), lista)) # prints col-1 col-2 col-3
or:
stra = ' '.join(s.rstrip(',') for s in lista) # prints col-1 col-2 col-3
I'm not entirely sure what you mean by "convert both the operations into 1", but if you don't have any control over lista, and can rely on it being in the format specified, then just do
' '.join(lista)[:-1]
If you do have control over lista, then follow nneonneo's suggestion and get rid of the commas in the list.
.rstrip()? There are no commas in yourListaitems..'a b c', there's no,?stra = ' '.join(lista)makes a string from a list. What more do you need?