Printing list elements on separate lines in Python

Python

Python Problem Overview


I am trying to print out Python path folders using this:

import sys
print sys.path

The output is like this:

>>> print sys.path
['.', '/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 do I print them into separate lines so I can parse them properly?

It should be like this:

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

Python Solutions


Solution 1 - Python

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

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

Solution 2 - Python

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

from __future__ import print_function

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

Solution 3 - Python

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']
>>> 

Solution 4 - Python

Use the splat operator (*).

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

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

Solution 5 - Python

for path in sys.path:
    print(path)

Solution 6 - Python

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.

Solution 7 - Python

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

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

Solution 8 - Python

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])

Solution 9 - Python

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']]

Solution 10 - Python

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])

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLarryView Question on Stackoverflow
Solution 1 - PythonSven MarnachView Answer on Stackoverflow
Solution 2 - PythonJBernardoView Answer on Stackoverflow
Solution 3 - PythonSingleNegationEliminationView Answer on Stackoverflow
Solution 4 - PythonGautam JagdhishView Answer on Stackoverflow
Solution 5 - PythonWinston EwertView Answer on Stackoverflow
Solution 6 - PythontravcView Answer on Stackoverflow
Solution 7 - PythonmirekphdView Answer on Stackoverflow
Solution 8 - PythonSuman SaurabhView Answer on Stackoverflow
Solution 9 - PythonbraulioView Answer on Stackoverflow
Solution 10 - PythonCyborgView Answer on Stackoverflow