Skip to Content

How To Print A List In Python (And With Custom Formatting)

When you want to print the contents of a list you have a couple of different options depending upon the context. If you’re in the REPL there’s the easy method of entering the name of the variable storing the list, to the option of using the standard print function.

If you’re within a script file the best option would be to use the print function, or if you want to import the sys library to write the equivalent of print by using sys.stdout.write().

Print From REPL

When writing code in the Python REPL you can easily inspect the contents of a variable by writing the name of the variable on its own line, like so:

>>> my_list = ['a', 1, { 'b': 2 }]
>>> my_list
['a', 1, {'b': 2}]

This is the easiest and fastest way of being able to print and see the contents of your list in the REPL.

Even if there is an exorbitant amount of spacing between each element, for whatever reason, when the result is printed to the REPL it will display it neatly as follows:

>>> my_list = ['a',    1,    {'b'    :    2}]
>>> my_list
['a', 1, {'b': 2}]

The other option to printing lists in Python which is available in the REPL is to just insert the variable containing the list in the print parameter, like so:

>>> my_list = ['a', 1, {'b': 2}]
>>> print(my_list)
['a', 1, {'b': 2}]

The print function is the most common usage of printing a list in Python as it can also be used in a Python file.

Print Each Element In List Individually

But what if you wanted to inspect the elements within a list individually?

One way within the REPL would be to use the print() function within a for loop, like so:

>>> my_list = ['a', 1, { 'b': 2 }]
>>> for item in my_list:
...     print(item)
...
a
1
{'b': 2}

Print List Using A Custom Format

What if you wanted to print the whole list in Python in a different way?

You can still use the print() function, but depending upon the type of need with your custom format you would need to convert the list into a string as this is the type needed to display the output with your custom modifications.

One way to achieve this is to use the map function by converting each element to a str and then join each element into a string, like so:

>>> my_list = ['a', 1, {'b': 2 }]
>>> print(f"[{'; '.join(map(str, my_list))}"])
['a'; 1; {'b': 2}]

Notice the change I made in the second line where I used semi-colon character to separate each element in the list?

You can change this to be whatever you like, even a tab character if you want!

>>> my_list = ['a', 1, {'b': 2 }]
>>> tab = '\t'
>>> print(f"[{tab.join(map(str, my_list))}"])
[a	1	{'b': 2}]

This method may help to more easily see each element within the list.

The reason why the tab short-cut string '\t' was inserted into its own variable was to help it be parsed by the f-string. This does not work:

>>> print(f"[{'\t'.join(map(str, my_list))}"])
 File "<input>", line 1
SyntaxError: f-string expression part cannot include a backslash

So to avoid this error with any strings containing the backslash (such as \n) just place the special character in a variable and reference that with your join function.

Print Using sys.stdout.write()

The equivalent of using the print function is to import the sys library and use the stdout.write() function, like so:

>>> import sys
>>> my_list = ['a', 1, {'b': 2}]
>>> sys.stdout.write(str(my_list))
['a', 1, {'b': 2}]

Notice the list was converted to a string. If you want to perform any particular formatting then you can use the methods already raised above, making sure the resulting output is of a string data type. If you don’t have a string in the stdout.write() function it will return a TypeError like so:

>>> import sys
>>> my_list = ['a', 1, {'b': 2}]
>>> sys.stdout.write(my_list)
  File "<input>", line 1, in <module>
TypeError: write() argument must be str, not list

Summary

There are various ways of being able to print a list in Python but it is dependent upon your context. In most scenarios using the print function is the best way.