Disclaimer: The question how to print a list in python is already covered multiple times, thus, this question does NOT ask HOW to print a list BUT WHETHER a specific way to print a list exists (using the format directive).
The first hit one gets googling how to print a list using format
in python is here and it looks like:
print('\n'.join('{}'.format(k) for k in lst))
is the way to go. But I keep wondering if there is a lisp like format directive to do this without the verbose join operation. E.g. in common lisp one would simple write:
(FORMAT T "~%~{~a~%~}" list-i-want-printed)
~{...~}
basically means iterate over list
~a
basically means to take one argument and print it using its (or the default) print/to-string directive
~%
newline
Does such a format directive exist in python?
As a more thorough example why I'd like to use such directive:
Given you have 3 lists you want to print below each other. The lisp FORMAT
would allow:
(FORMAT T "~{~a~}~%~{~a~}~%~{~a~}~%" list-1 list-2 list-3)
whereas the python solution would look like:
print(''.join('{}'.format(k) for k in lsta) + '\n' + ''.join('{}'.format(k) for k in lstb) + '\n' + ''.join('{}'.format(k) for k in lstc))
not quite as refined.
I think the basic answer is "No", but you can refine your example a bit:
print( ' '.join( '{}'.format(k) for k in lsta+lstb+lstc ) )
No newlines. If I wanted a newline after each list I'd do
for lst in (lsta,lstb,lstc):
print( ' '.join( '{}'.format(k) for k in lst ) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With