Trying to print list items on individual lines as simply as possible following this:
https://www.geeksforgeeks.org/print-lists-in-python-4-different-ways/
>>> myVar = [1, 2, 3, 4, 5]
>>> print(*myVar)
File "<stdin>", line 1
print(*myVar)
^
SyntaxError: invalid syntax
I must use Python 2.7.8, so I guess I have to translate that without ()
but I fail at this:
>>> print *myVar
File "<stdin>", line 1
print *myVar
^
SyntaxError: invalid syntax
So, is this a syntax issue? Or is there a better way to do this on v2.7.8?
Python 2's print
statement doesn't support argument unpacking (because it's not a function, so it has no arguments), but you can opt-in to using Python 3's print
function even in Python 2, by adding the following to the very top of your file (before any line aside from a shebang line and file encoding comments I believe; __future__
imports are weird, and since they change the compiler behavior, they need to occur before any other code):
from __future__ import print_function
Once you do that, the print
statement ceases to exist for that script, and you can use Python 3's print
function normally, e.g. for your desired task of printing the values of myVar
, one per line, you'd do:
print(*myVar, sep='\n')