04-22-2020, 11:39 PM
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:
Here’s the code:
Try It Yourself in Our Interactive Code Shell:
https://repl.it/@finxter/pythonlistprint3?lite=true
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: Use Default print() Statement
The default 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 . This is the standard list representation.
The output is the following:
Advantages |
Disadvantages
Easy to read and write |
Non-customizable
Fast |
Concise |
Try It Yourself in Our Interactive Code Shell:
https://repl.it/@finxter/printpythonlist1?lite=true
The next method overcomes the main disadvantage of being not very customizable.
Method: 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.
The output is the following:
Advantages |
Disadvantages
Fully customizable |
Relatively slow
Simple |
Less concise
|
Newline after each element
Try It Yourself in Our Interactive Python Shell:
https://repl.it/@finxter/printpythonlist2?lite=true
Method: 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 argument of the function that defines which character is added after each element that was printed to the shell (default: new-line character ):
The output is:
You see that the 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.
Advantages |
Disadvantages
Fully customizable |
Relatively slow
Simple |
Less concise
Try It Yourself in Our Interactive Code Shell:
https://repl.it/@finxter/printpythonlist2?lite=true
Let’s overcome the disadvantage of the for loop of being less concise!
Method: Unpacking With Separator Argument
The 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 argument of the function to define how to separate two elements of the iterable.
The 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.
Advantages |
Disadvantages
Fully customizable |
Harder to read for beginners
Fast |
Concise |
Try It Yourself in Our Interactive Code Shell:
https://repl.it/@finxter/pythonlistprint3?lite=true
This is the best and most Pythonic way to print a Python list. If you still want to learn about alternatives, keep reading.
Method: Use the string.join() Method
The method joins together all elements in the , using the as a separator between two elements. Thus, it works exactly like the argument of the function.
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 Pythonthrows an error:
Advantages |
Disadvantages
Fully customizable |
Harder to read for beginners
Concise |
Slow
|
Works only for string elements
Try It Yourself in Our Interactive Code Shell:
https://repl.it/@finxter/printpythonlists4?lite=true
So how do you apply this method to integer lists?
Method: Use the string.join() Method with Map()
The method joins together all elements in the , using the as a separator between two elements. But it expects that all elements in the are already strings. If they aren’t, you need to convert them first. To achieve this, you can use the built-in method in Python 3.x.
The method applies the function to each element in the list. In other words, it converts each integer element to a string. An alternative way without the function would be list comprehension that results in the same output.
Advantages |
Disadvantages
Fully customizable |
Harder to read for beginners
Concise |
Slow
Works for all data types |
Try It Yourself in Our Interactive Code Shell:
https://repl.it/@finxter/printpythonlists5?lite=true
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!
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 function in Python.Code:
print()
- Use the asterisk operatorin front of the list to “unpack” the list into the print function.Code:
*
- Use the argument to define how to separate two list elements visually.Code:
sep
Here’s the code:
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-->5Try It Yourself in Our Interactive Code Shell:
https://repl.it/@finxter/pythonlistprint3?lite=true
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: Use Default print() Statement
The default
Code:
print()Code:
[Code:
]Code:
a, bCode:
lst = [1, 2, 3, 4, 5]
print(lst)The output is the following:
Code:
[1, 2, 3, 4, 5]Advantages |
Disadvantages
Easy to read and write |
Non-customizable
Fast |
Concise |
Try It Yourself in Our Interactive Code Shell:
https://repl.it/@finxter/printpythonlist1?lite=true
The next method overcomes the main disadvantage of being not very customizable.
Method: 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.
Code:
# 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:
Code:
Element: 1
Element: 2
Element: 3
Element: 4
Element: 5Advantages |
Disadvantages
Fully customizable |
Relatively slow
Simple |
Less concise
|
Newline after each element
Try It Yourself in Our Interactive Python Shell:
https://repl.it/@finxter/printpythonlist2?lite=true
Method: 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
Code:
endCode:
print()Code:
\nCode:
# 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:
Code:
1 2 3 4 5You see that the
Code:
endAdvantages |
Disadvantages
Fully customizable |
Relatively slow
Simple |
Less concise
Try It Yourself in Our Interactive Code Shell:
https://repl.it/@finxter/printpythonlist2?lite=true
Let’s overcome the disadvantage of the for loop of being less concise!
Method: Unpacking With Separator Argument
The
Code:
print()Code:
*Code:
sepCode:
print()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-->5The
Code:
sepAdvantages |
Disadvantages
Fully customizable |
Harder to read for beginners
Fast |
Concise |
Try It Yourself in Our Interactive Code Shell:
https://repl.it/@finxter/pythonlistprint3?lite=true
This is the best and most Pythonic way to print a Python list. If you still want to learn about alternatives, keep reading.
Method: Use the string.join() Method
The
Code:
string.join(iterable)Code:
iterableCode:
stringCode:
sepCode:
print()Code:
# 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-->5Note 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 Pythonthrows an error:
Code:
TypeError: sequence item 0: expected str instance, int foundAdvantages |
Disadvantages
Fully customizable |
Harder to read for beginners
Concise |
Slow
|
Works only for string elements
Try It Yourself in Our Interactive Code Shell:
https://repl.it/@finxter/printpythonlists4?lite=true
So how do you apply this method to integer lists?
Method: Use the string.join() Method with Map()
The
Code:
string.join(iterable)Code:
iterableCode:
stringCode:
iterableCode:
map()Code:
# 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-->5The
Code:
map(str, lst)Code:
str(x)Code:
xCode:
map(str, lst)Code:
[str(x) for x in lst]Advantages |
Disadvantages
Fully customizable |
Harder to read for beginners
Concise |
Slow
Works for all data types |
Try It Yourself in Our Interactive Code Shell:
https://repl.it/@finxter/printpythonlists5?lite=true
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!

