Larry
Larry

Reputation: 1449

Printing list elements on separate lines in Python

When I try this code:

import sys
print sys.path

I get an output like:

['.', '/usr/bin', '/home/student/Desktop', '/home/student/my_modules', '/usr/lib/pyth
on2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/pyth
on2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-pack
ages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/
usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/
python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/p
ython2.6/dist-packages/wx-2.8-gtk2-unicode']

How can I print each element of the list on a separate line, like so?

/usr/bin
/home/student/Desktop
/home/student/my_modules

Upvotes: 142

Views: 315369

Answers (10)

Cyborg
Cyborg

Reputation: 1467

For printing list elements on separate lines, you can use:

files = ['test1.txt', 'test2.txt', 'test3.txt']
for i in range(len(files)): print(files[i])

Upvotes: 2

Winston Ewert
Winston Ewert

Reputation: 45059

for path in sys.path:
    print(path)

Upvotes: 19

braulio
braulio

Reputation: 571

You can also turn your list into a numpy array of size len(sys.path)

print(np.array(sys.path).reshape(-1,1))

outputs:

[['.']
 ['/usr/bin']
 ['/home/student/Desktop']
 ['/home/student/my_modules']
 ['/usr/lib/python2.6']
 ['/usr/lib/python2.6/plat-linux2']
 ['/usr/lib/python2.6/lib-tk']
 ['/usr/lib/pyton2.6/lib-old']
 ['/usr/lib/python2.6/lib-dynload']
 ['/usr/local/lib/python2.6/dist-packages']
 ['/usr/lib/python2.6/dist-packages']
 ['/usr/lib/python2.6/dist-packages/PIL']
 ['/usr/lib/python2.6/dist-packages/gst-0.10']
 ['/usr/lib/pymodules/python2.6']
 ['/usr/lib/python2.6/dist-packages/gtk-2.0']
 ['/usr/lib/pymodules/python2.6/gtk-2.0']
 ['/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode']]

Upvotes: 0

gautam
gautam

Reputation: 449

Use the splat operator (*).

By default, print prints arguments separated by space. Use sep argument to specify the delimiter:

print(*sys.path, sep="\n")

Upvotes: 31

mirekphd
mirekphd

Reputation: 6841

A slightly more general solution based on join, that works even for pandas.Timestamp:

print("\n".join(map(str, my_list)))

Upvotes: 4

Suman Saurabh
Suman Saurabh

Reputation: 31

sys.path returns the list of paths

ref

sys.path

A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.

As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.

import sys
dirs=sys.path
for path in dirs:
   print(path)

or you can print only first path by

print(dir[0])

Upvotes: 1

JBernardo
JBernardo

Reputation: 33407

Use the print function (Python 3.x) or import it (Python 2.6+):

from __future__ import print_function

print(*sys.path, sep='\n')

Upvotes: 108

Sven Marnach
Sven Marnach

Reputation: 602525

print("\n".join(sys.path))

(The outer parentheses are included for Python 3 compatibility and are usually omitted in Python 2.)

Upvotes: 242

SingleNegationElimination
SingleNegationElimination

Reputation: 156278

Another good option for handling this kind of option is the pprint module, which (among other things) pretty prints long lists with one element per line:

>>> import sys
>>> import pprint
>>> pprint.pprint(sys.path)
['',
 '/usr/lib/python27.zip',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7/site-packages',
 '/usr/lib/python2.7/site-packages/PIL',
 '/usr/lib/python2.7/site-packages/gst-0.10',
 '/usr/lib/python2.7/site-packages/gtk-2.0',
 '/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info',
 '/usr/lib/python2.7/site-packages/webkit-1.0']
>>> 

Upvotes: 24

travc
travc

Reputation: 1859

Sven Marnach's answer is pretty much it, but has one generality issue... It will fail if the list being printed doesn't just contain strings.

So, the more general answer to "How to print out a list with elements separated by newlines"...

print '\n'.join([ str(myelement) for myelement in mylist ])

Then again, the print function approach JBernardo points out is superior. If you can, using the print function instead of the print statement is almost always a good idea.

Upvotes: 9

Related Questions