I want to turn this list:
['1','2','3','4']
into this string:
"%(1)s, %(2)s, %(3)s, %(4)s"
How can I do this, preferrably in ONE LINE?
I want to turn this list:
['1','2','3','4']
into this string:
"%(1)s, %(2)s, %(3)s, %(4)s"
How can I do this, preferrably in ONE LINE?
Using string formatting, str.join()
and a list comprehension:
', '.join(['%({})s'.format(i) for i in inputlist])
Demo:
>>> inputlist = ['1','2','3','4']
>>> ', '.join(['%({})s'.format(i) for i in inputlist])
'%(1)s, %(2)s, %(3)s, %(4)s'
See List comprehension without [ ] in Python why it is better to use a list comprehension, and not a generator expression here.