4 Ways to Print a Python List

Using the built-in print() function to print a list in Python results in an unformatted output. However, you may want to control what the output looks like. That’s why I decided to show you four different methods to print list elements in Python.

Covered methods will show you how to print a list using:

  • For loop
  • * operator
  • join() and map() methods
  • pprint() method

But, before going further, let’s first remember what Python list is exactly.

What is a Python list?

Python list is one of the most commonly used data structures in Python. A list is a sequence of items that can be accessed by an index. It is enclosed by square brackets and the items are separated by commas. 

The list can be created in Python by putting elements inside square brackets or using the list() function. The following example shows what the Python list looks like. Notice the output we got by using the built-in print() function. It is a list printed as string, containing square brackets and commas too.

a = [10, 20, 'abc', 30]
print(a)

# Output
[10, 20, 'abc', 30]

Ok, now we are ready to learn how to print the list elements in a better way.

Python print list – 4 methods

1. Using For loop

This is the most commonly used method since for loops are one of the first things you learn in Python. You can print list elements by iterating over each element and printing it using the built-in print() function. If no additional parameters are defined, each list item is printed on a separate line.

In our example it looks like this.

a = [10, 20, 'abc', 30]
for item in a:
  print(item)

# Output
10
20
abc
30

If you don’t want to print list elements line by line, you can define the ending character manually by setting the value for end parameter. This parameter defines a string that is concatenated to the outputted string after each print() call. So if we want a whitespace being outputted instead of newline after each list element, we can define it as end=' '.

a = [10, 20, 'abc', 30]
for item in a:
  print(item, end=' ')

# Output
10 20 abc 30

2. Using the * operator

Unpacking operator, also known as * (asterisk) operator, unpacks values from any iterable object such as list, tuple, string, etc. It is used to convert list elements to separate arguments that are passed to a function where it is used. 

Using the * operator inside the print() function will result in list items being outputted in the same line separated with spaces. You can control the separator by defining the sep parameter inside the print() function.

Example 1 – using the * operator.

a = [10, 20, 'abc', 30]
print(*a)

# Output
10 20 abc 30

Example 2 – using the * operator with sep=', ' parameter.

a = [10, 20, 'abc', 30]
print(*a, sep=", ")

# Output
10, 20, abc, 30

Example 3 – using the * operator with sep='\n' parameter.

a = [10, 20, 'abc', 30]
print(*a, sep="\n")

# Output
10
20
abc
30

3. Using join() and map() methods

Ideally, if a list contains only strings, you could use the join() method. It joins list items and returns the combined string. A separator is defined as the initial string over which the join() method is called. 

If other data types are parts of the list, such as integers, bools, or objects, then you’ll have first to convert these types into strings too. You can do that by using the map() method. Either way, the result of calling the join() method will be a new string.

Example 1 – using the join() method on a list containing only strings.

a = ['abc', 'def', 'ghi']
print(', '.join(a))

# Output
abc, def, ghi

Example 2 – using the join() method on a list containing mixed data types resulting in a TypeError.

a = [10, 20, 'abc', True]
print(', '.join(a))

# Output
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, int found

Example 3 – using the join() and map() methods on a list containing mixed data types.

a = [10, 20, 'abc', True]
print(', '.join(map(str, a)))

# Output
10, 20, abc, True
NOTE - Remember, map(fun, iter) applies a function fun on all the items of an iterator iter given as input. In our example, it applies str() function over each list element.

As you can see, the map() method is used to convert each list item to a string. Once the list contains only strings, the join() method combines them into a new string.

4. Using the pprint() method

If a list contains more complex data, such as nested lists or dictionaries with different depths, then Python’s pprint module is the way to go. It is used to pretty-print complex data structures, offering various parameters for fine-tuning the output.

To demonstrate how it works, we’ll define a list containing multiple objects (dicts).

a = [{'street': '9278 new road', 'city': 'kilcoole', 'state': 'waterford', 'postcode': '93027', 'coordinates': {'latitude': '20.9267', 'longitude': '-7.9310'}, 'timezone': {'offset': '-3:30', 'description': 'Newfoundland'}}, {'title': 'mr', 'first': 'brad', 'last': 'gibson'}]
print(a)

# Output
[{'street': '9278 new road', 'city': 'kilcoole', 'state': 'waterford', 'postcode': '93027', 'coordinates': {'latitude': '20.9267', 'longitude': '-7.9310'}, 'timezone': {'offset': '-3:30', 'description': 'Newfoundland'}}, {'title': 'mr', 'first': 'brad', 'last': 'gibson'}]

The output is not quite readable, right. To make it reader-friendly, we’ll use the pprint module. The width parameter defines the maximum width of each printed line. This way we can print list of objects in Python without much hassle.

import pprint
pp = pprint.PrettyPrinter(width=60)
pp.pprint(a)

# Output
[{'city': 'kilcoole',
  'coordinates': {'latitude': '20.9267',
                  'longitude': '-7.9310'},
  'postcode': '93027',
  'state': 'waterford',
  'street': '9278 new road',
  'timezone': {'description': 'Newfoundland',
               'offset': '-3:30'}},
 {'first': 'brad', 'last': 'gibson', 'title': 'mr'}]

Another useful parameter is depth which defines how deep objects should be printed. Everything at a deeper level than the depth value will be omitted from the output. Various parameters within the pprint module allow you to format the printing of the list of objects in Python.

import pprint
pp = pprint.PrettyPrinter(width=60, depth=2)
pp.pprint(a)

# Output
[{'city': 'kilcoole',
  'coordinates': {...},
  'postcode': '93027',
  'state': 'waterford',
  'street': '9278 new road',
  'timezone': {...}},
 {'first': 'brad', 'last': 'gibson', 'title': 'mr'}]

You can find other formatting parameters for the pprint module in Python’s official docs.