Using for in one-liner

Paul Watson pwatson at redlinepy.com
Mon Aug 15 18:29:52 CEST 2005


Can a for loop be used in a one-liner?  What am I missing?

$ python -c "import sys;print ''.join([line for line in 
sys.stdin.readlines()]),"
now is
the time
now is
the time

$ python -c "import sys;for line in sys.stdin.readlines(): print line,"
   File "<string>", line 1
     import sys;for line in sys.stdin.readlines(): print line,
                  ^
SyntaxError: invalid syntax

$ python -c "import sys;for i in range(5): print i,"
   File "<string>", line 1
     import sys;for i in range(5): print i,
                  ^
SyntaxError: invalid syntax



More information about the Python-list mailing list