Create an account


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]

<div><p>How to print a Python list in a beautiful and fully customizable way? </p>
<p>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.</p>
<p><strong>If you just want to know the best way to print a list in Python, here’s the short answer:</strong></p>
<ul>
<li><strong>Pass a list as an input to the <code>print()</code> function in Python. </strong></li>
<li><strong>Use the <a rel="noreferrer noopener" href="https://blog.finxter.com/what-is-asterisk-in-python/" target="_blank">asterisk operator </a><code>*</code> in front of the list to “unpack” the list into the print function. </strong></li>
<li><strong>Use the <code>sep</code> argument to define how to separate two list elements visually. </strong></li>
</ul>
<p>Here’s the code:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># 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</pre>
<p>Try It Yourself in Our Interactive Code Shell:</p>
<p> <iframe src="https://repl.it/@finxter/pythonlistprint3?lite=true" scrolling="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals" width="100%" height="700px" frameborder="no"></iframe> </p>
<p>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!</p>
<h2>Method: Use Default print() Statement</h2>
<p>The default <code>print()</code> statement converts the list into a string representation that encloses the list elements in the square brackets <code>[</code> and <code>]</code>, and separates two subsequent elements with the comma and an empty space <code>a, b</code>. This is the standard list representation.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">lst = [1, 2, 3, 4, 5]
print(lst)</pre>
<p>The output is the following:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[1, 2, 3, 4, 5]</pre>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Advantages</th>
<th>Disadvantages</th>
</tr>
</thead>
<tbody>
<tr>
<td>Easy to read and write</td>
<td>Non-customizable</td>
</tr>
<tr>
<td>Fast</td>
<td></td>
</tr>
<tr>
<td>Concise</td>
<td></td>
</tr>
</tbody>
</table>
</figure>
<p>Try It Yourself in Our Interactive Code Shell:</p>
<p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/printpythonlist1?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<p>The next method overcomes the main disadvantage of being not very customizable.</p>
<h2>Method: Iterate In a For Loop</h2>
<p>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.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># 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)</pre>
<p>The output is the following:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Element: 1
Element: 2
Element: 3
Element: 4
Element: 5</pre>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Advantages</th>
<th>Disadvantages</th>
</tr>
</thead>
<tbody>
<tr>
<td>Fully customizable</td>
<td>Relatively slow</td>
</tr>
<tr>
<td>Simple</td>
<td>Less concise</td>
</tr>
<tr>
<td></td>
<td>Newline after each element</td>
</tr>
</tbody>
</table>
</figure>
<p>Try It Yourself in Our Interactive Python Shell:</p>
<p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/printpythonlist2?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<h2>Method: Iterate in For Loop with End Argument</h2>
<p>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</code> argument of the <code>print()</code> function that defines which character is added after each element that was printed to the shell (default: new-line character <code>\n</code>):</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># 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=' ')</pre>
<p>The output is:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1 2 3 4 5 </pre>
<p>You see that the <code>end</code> 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.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Advantages</th>
<th>Disadvantages</th>
</tr>
</thead>
<tbody>
<tr>
<td>Fully customizable</td>
<td>Relatively slow</td>
</tr>
<tr>
<td>Simple</td>
<td>Less concise</td>
</tr>
</tbody>
</table>
</figure>
<p>Try It Yourself in Our Interactive Code Shell:</p>
<p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/printpythonlist2?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<p>Let’s overcome the disadvantage of the for loop of being less concise!</p>
<h2 id="block-d62f54a1-4fa6-4203-82ef-149dc9786c9f">Method: Unpacking With Separator Argument</h2>
<p>The <code>print()</code> function works with an iterable as input. You can use the <a href="https://blog.finxter.com/what-is-asterisk-in-python/" target="_blank" rel="noreferrer noopener">asterisk operator </a><code>*</code> in front of the list to “unpack” the list into the print function. Now, you can use the <code>sep</code> argument of the <code>print()</code> function to define how to separate two elements of the iterable.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># 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</pre>
<p>The <code>sep</code> 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.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Advantages</th>
<th>Disadvantages</th>
</tr>
</thead>
<tbody>
<tr>
<td>Fully customizable</td>
<td>Harder to read for beginners</td>
</tr>
<tr>
<td>Fast</td>
<td></td>
</tr>
<tr>
<td>Concise</td>
<td></td>
</tr>
</tbody>
</table>
</figure>
<p>Try It Yourself in Our Interactive Code Shell:</p>
<p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/pythonlistprint3?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<p>This is the best and most Pythonic way to print a Python list. If you still want to learn about alternatives, keep reading.</p>
<h2 id="block-d62f54a1-4fa6-4203-82ef-149dc9786c9f">Method: Use the string.join() Method</h2>
<p>The <code>string.join(iterable)</code> method joins together all elements in the <code>iterable</code>, using the <code>string</code> as a separator between two elements. Thus, it works exactly like the <code>sep</code> argument of the <code>print()</code> function. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># 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
</pre>
<p>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 <a href="https://blog.finxter.com/python-crash-course/" target="_blank" rel="noreferrer noopener">Python </a>throws an error:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">TypeError: sequence item 0: expected str instance, int found</pre>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Advantages</th>
<th>Disadvantages</th>
</tr>
</thead>
<tbody>
<tr>
<td>Fully customizable</td>
<td>Harder to read for beginners</td>
</tr>
<tr>
<td>Concise</td>
<td>Slow</td>
</tr>
<tr>
<td></td>
<td>Works only for string elements</td>
</tr>
</tbody>
</table>
</figure>
<p>Try It Yourself in Our <a href="https://blog.finxter.com/how-can-i-embed-python-code-in-an-xml-file/" target="_blank" rel="noreferrer noopener">Interactive Code Shell</a>:</p>
<p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/printpythonlists4?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<p>So how do you apply this method to integer <a href="https://blog.finxter.com/python-lists/" target="_blank" rel="noreferrer noopener">lists</a>?</p>
<h2 id="block-d62f54a1-4fa6-4203-82ef-149dc9786c9f">Method: Use the string.join() Method with Map()</h2>
<p>The <code>string.join(iterable)</code> method joins together all elements in the <code>iterable</code>, using the <code>string</code> as a separator between two elements. But it expects that all elements in the <code>iterable</code> are already strings. If they aren’t, you need to convert them first. To achieve this, you can use the <a href="https://blog.finxter.com/which-is-faster-list-comprehension-or-map-function-in-python/" target="_blank" rel="noreferrer noopener">built-in <code>map()</code> method</a> in Python 3.x.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># 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
</pre>
<p>The <code>map(str, lst)</code> method applies the function <code>str(x)</code> to each element <code>x</code> in the list. In other words, it converts each integer element to a string. An alternative way without the <code>map(str, lst)</code> function would be <a href="https://blog.finxter.com/list-comprehension/" target="_blank" rel="noreferrer noopener">list comprehension</a> <code>[str(x) for x in lst]</code> that results in the same output.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Advantages</th>
<th>Disadvantages</th>
</tr>
</thead>
<tbody>
<tr>
<td>Fully customizable</td>
<td>Harder to read for beginners</td>
</tr>
<tr>
<td>Concise</td>
<td>Slow</td>
</tr>
<tr>
<td>Works for all data types</td>
<td></td>
</tr>
</tbody>
</table>
</figure>
<p>Try It Yourself in Our Interactive Code Shell:</p>
<p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/printpythonlists5?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<p>So, let’s finish this up!</p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>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?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>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.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p></p>
</div>


https://www.sickgaming.net/blog/2020/04/...-run-code/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016