I very like quite new Python convention to print things with .format()
Is it possible using it to print element line by line. Assuming of course number of elements is unknown.
Working example will be appreciated.
Without using loops: * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by space use sep=”\n” or sep=”, ” respectively.
To print a list and display it in a single line, a straightforward solution is to iterate over each element in a for loop and print this element into the same line using the print() function with the end=' ' argument set to the empty space.
If you are using Python 3.x and your intention is to just printing the list of elements, one in each line, then you can use print
function itself, like this
my_list = [1, 2, 3, 4]
print(*my_list, sep="\n")
*my_list
simply unpacks the list elements and pass each one of them as parameters to the print
function (Yes, print
is a function in Python 3.x).
Output
1
2
3
4
If you are using Python 2.x, then you can just import the print function from the future like this
from __future__ import print_function
Note: This import should be the first line in the file.
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