Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Print a Python List Beautifully [Click & Run Code]
1
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
    Code:
    print()
    function in Python.


  • Use the asterisk operator
    Code:
    *
    in front of the list to “unpack” the list into the print function.


  • Use the
    Code:
    sep
    argument to define how to separate two list elements visually.


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-->5

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
Code:
print()
statement converts the list into a string representation that encloses the list elements in the square brackets
Code:
[
and
Code:
]
, and separates two subsequent elements with the comma and an empty space
Code:
a, b
. This is the standard list representation.

Code:
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: 5

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
Code:
end
argument of the
Code:
print()
function that defines which character is added after each element that was printed to the shell (default: new-line character
Code:
\n
):

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: # Use the end argument to define # what to print after each element print(str(x), end=' ')

The output is:

Code:
1 2 3 4 5

You see that the
Code:
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.

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
Code:
print()
function works with an iterable as input. You can use the asterisk operator
Code:
*
in front of the list to “unpack” the list into the print function. Now, you can use the
Code:
sep
argument of the
Code:
print()
function to define how to separate two elements of the iterable.

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

The
Code:
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.

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
Code:
string.join(iterable)
method joins together all elements in the
Code:
iterable
, using the
Code:
string
as a separator between two elements. Thus, it works exactly like the
Code:
sep
argument of the
Code:
print()
function.

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-->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 Pythonthrows an error:

Code:
TypeError: sequence item 0: expected str instance, int found

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

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-->5

The
Code:
map(str, lst)
method applies the function
Code:
str(x)
to each element
Code:
x
in the list. In other words, it converts each integer element to a string. An alternative way without the
Code:
map(str, lst)
function would be list comprehension
Code:
[str(x) for x in lst]
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!
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: