A tuple is an iterable data type in Python, so you can write a for loop to print the items in it.
By setting sep to a newline this'll print all list objects on new lines. This'll omit the newline after 'The lists are:' , you can always use sep='\n' here as well. Using *L is more efficient since it doesn't have to join the items into a new string. print just loops over the args, writing each to stdout .
You can put a for loop inside a while, or a while inside a for, or a for inside a for, or a while inside a while. Or you can put a loop inside a loop inside a loop. You can go as far as you want.
Python print() Function The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.
I have a question about Python (3.3.2).
I have a list:
L = [['some'], ['lists'], ['here']]
I want to print these nested lists (each one on a new line) using the print()
function:
print('The lists are:', for list in L: print(list, '\n'))
I know this is incorrect but I hope you get the idea. Could you please tell me if this is possible? If yes, how?
I know that I could do this:
for list in L:
print(list)
However, I'd like to know if there are other options as well.
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