Print a Python List Beautifully [Click & Run Code]

How to print a Python list in a beautiful and fully customizable way?

This article shows you six effective ways of doing it. By studying these alternatives, you’ll not only learn how to print lists in Python, you’ll become a better coder overall.

If you just want to know the best way to print a list in Python, here’s the short answer:

  • Pass a list as an input to the print() function in Python.
  • Use the asterisk operator * in front of the list to “unpack” the list into the print function.
  • Use the sep argument to define how to separate two list elements visually.

Here’s the code:

# Create the Python List
lst = [1, 2, 3, 4, 5]

# Use three underscores as separator
print(*lst, sep='___')
# 1___2___3___4___5

# Use an arrow as separator
print(*lst, sep='-->')
# 1-->2-->3-->4-->5

Try It Yourself in Our Interactive Code Shell:

This is the best and most Pythonic way to print a Python list. If you still want to learn about alternatives—and improve your Python skills in the process of doing so—keep reading!

Method 1: Use Default print() Statement

The default print() statement converts the list into a string representation that encloses the list elements in the square brackets [ and ], and separates two subsequent elements with the comma and an empty space a, b. This is the standard list representation.

lst = [1, 2, 3, 4, 5]
print(lst)

The output is the following:

[1, 2, 3, 4, 5]
AdvantagesDisadvantages
Easy to read and writeNon-customizable
Fast
Concise

Try It Yourself in Our Interactive Code Shell:

The next method overcomes the main disadvantage of being not very customizable.

Method 2: Iterate In a For Loop

If you want full control about the output of each list element, you can use the straightforward approach of using a for loop to iterate over each element x in the list. You can then decide for yourself how to print each element.

# Create the Python List
lst = [1, 2, 3, 4, 5]

# Iterate over each element x
# in the list and customize printing
for x in lst:
    print('Element: ' + x)

The output is the following:

Element: 1
Element: 2
Element: 3
Element: 4
Element: 5
AdvantagesDisadvantages
Fully customizableRelatively slow
SimpleLess concise
Newline after each element

Try It Yourself in Our Interactive Python Shell:

Method 3: Iterate in For Loop with End Argument

If you’d rather print all elements in a single line, separated by three whitespace characters, you can do so by defining the end argument of the print() function that defines which character is added after each element that was printed to the shell (default: new-line character \n):

# Create the Python List
lst = [1, 2, 3, 4, 5]

# Iterate over each element x
# in the list and customize printing
for x in lst:
    # Use the end argument to define
    # what to print after each element
    print(str(x), end='   ')

The output is:

1   2   3   4   5   

You see that the end argument overwrites the default behavior of printing a new-line character at the end of each element. Instead, each two elements are separated by three empty spaces.

AdvantagesDisadvantages
Fully customizableRelatively slow
SimpleLess concise

Try It Yourself in Our Interactive Code Shell:

Let’s overcome the disadvantage of the for loop of being less concise!

Method 4: Unpacking With Separator Argument

The print() function works with an iterable as input. You can use the asterisk operator * in front of the list to “unpack” the list into the print function. Now, you can use the sep argument of the print() function to define how to separate two elements of the iterable.

# Create the Python List
lst = [1, 2, 3, 4, 5]

# Use three underscores as separator
print(*lst, sep='___')
# 1___2___3___4___5

# Use an arrow as separator
print(*lst, sep='-->')
# 1-->2-->3-->4-->5

The sep argument allows you to define precisely what to put between each pair of elements in an iterable. This allows you full customization and keeps the code lean and concise.

AdvantagesDisadvantages
Fully customizableHarder to read for beginners
Fast
Concise

Try It Yourself in Our Interactive Code Shell:

This is the best and most Pythonic way to print a Python list. If you still want to learn about alternatives, keep reading.

Method 5: Use the string.join() Method

The string.join(iterable) method joins together all elements in the iterable, using the string as a separator between two elements. Thus, it works exactly like the sep argument of the print() function.

# Create the Python List
lst = ['1', '2', '3', '4', '5']

# Use three underscores as separator
print('___'.join(lst))
# 1___2___3___4___5

# Use arrow as separator
print('-->'.join(lst))
# 1-->2-->3-->4-->5

Note that you can only use this methods if the list elements are already strings. If they are integers, joining them together doesn’t work and Python throws an error:

TypeError: sequence item 0: expected str instance, int found
AdvantagesDisadvantages
Fully customizableHarder to read for beginners
ConciseSlow
Works only for string elements

Try It Yourself in Our Interactive Code Shell:

So how do you apply this method to integer lists?

Method 6: Use the string.join() Method with Map()

The string.join(iterable) method joins together all elements in the iterable, using the string as a separator between two elements. But it expects that all elements in the iterable are already strings. If they aren’t, you need to convert them first. To achieve this, you can use the built-in map() method in Python 3.x.

# Create the Python List
lst = [1, 2, 3, 4, 5]

# Use three underscores as separator
print('___'.join(map(str, lst)))
# 1___2___3___4___5

# Use arrow as separator
print('-->'.join(map(str, lst)))
# 1-->2-->3-->4-->5

The map(str, lst) method applies the function str(x) to each element x in the list. In other words, it converts each integer element to a string. An alternative way without the map(str, lst) function would be list comprehension [str(x) for x in lst] that results in the same output.

AdvantagesDisadvantages
Fully customizableHarder to read for beginners
ConciseSlow
Works for all data types

Try It Yourself in Our Interactive Code Shell:

So, let’s finish this up!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!