How can I format a list to print each element on a separate line?
For example I have:
mylist = ['10', '12', '14']
and I wish to format the list so it prints like this:
10 12 14
so the n, brackets, commas and ” is removed and each element is printed on a separate line
Thanks
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Embrace the future! Just to be complete, you can also do this the Python 3k way by using the print function:
from __future__ import print_function # Py 2.6+; In Py 3k not needed mylist = ['10', 12, '14'] # Note that 12 is an int print(*mylist,sep='n')
Prints:
10 12 14
Eventually, print as Python statement will go away… Might as well start to get used to it.
Method 2
Use str.join:
In [27]: mylist = ['10', '12', '14'] In [28]: print 'n'.join(mylist) 10 12 14
Method 3
You can just use a simple loop: –
>>> mylist = ['10', '12', '14']
>>> for elem in mylist:
print elem
10
12
14
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0