{"id":111885,"date":"2020-04-22T09:08:48","date_gmt":"2020-04-22T09:08:48","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=7760"},"modified":"2020-04-22T09:08:48","modified_gmt":"2020-04-22T09:08:48","slug":"print-a-python-list-beautifully-click-run-code","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/04\/22\/print-a-python-list-beautifully-click-run-code\/","title":{"rendered":"Print a Python List Beautifully [Click &amp; Run Code]"},"content":{"rendered":"<p>How to print a Python list in a beautiful and fully customizable way? <\/p>\n<p>This article shows you six effective ways of doing it. By studying these alternatives, you&#8217;ll not only learn how to print lists in Python, you&#8217;ll become a better coder overall.<\/p>\n<p><strong>If you just want to know the best way to print a list in Python, here&#8217;s the short answer:<\/strong><\/p>\n<ul>\n<li><strong>Pass a list as an input to the <code>print()<\/code> function in Python. <\/strong><\/li>\n<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 &#8220;unpack&#8221; the list into the print function. <\/strong><\/li>\n<li><strong>Use the <code>sep<\/code> argument to define how to separate two list elements visually. <\/strong><\/li>\n<\/ul>\n<p>Here&#8217;s the code:<\/p>\n<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\nlst = [1, 2, 3, 4, 5] # Use three underscores as separator\nprint(*lst, sep='___')\n# 1___2___3___4___5 # Use an arrow as separator\nprint(*lst, sep='-->')\n# 1-->2-->3-->4-->5<\/pre>\n<p>Try It Yourself in Our Interactive Code Shell:<\/p>\n<p> <iframe loading=\"lazy\" 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>\n<p>This is the best and most Pythonic way to print a Python list. If you still want to learn about alternatives&#8212;and improve your Python skills in the process of doing so&#8212;keep reading!<\/p>\n<h2>Method: Use Default print() Statement<\/h2>\n<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>\n<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]\nprint(lst)<\/pre>\n<p>The output is the following:<\/p>\n<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>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Advantages<\/th>\n<th>Disadvantages<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Easy to read and write<\/td>\n<td>Non-customizable<\/td>\n<\/tr>\n<tr>\n<td>Fast<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Concise<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>Try It Yourself in Our Interactive Code Shell:<\/p>\n<p> <iframe loading=\"lazy\" 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>\n<p>The next method overcomes the main disadvantage of being not very customizable.<\/p>\n<h2>Method: Iterate In a For Loop<\/h2>\n<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>\n<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\nlst = [1, 2, 3, 4, 5] # Iterate over each element x\n# in the list and customize printing\nfor x in lst: print('Element: ' + x)<\/pre>\n<p>The output is the following:<\/p>\n<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\nElement: 2\nElement: 3\nElement: 4\nElement: 5<\/pre>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Advantages<\/th>\n<th>Disadvantages<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Fully customizable<\/td>\n<td>Relatively slow<\/td>\n<\/tr>\n<tr>\n<td>Simple<\/td>\n<td>Less concise<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>Newline after each element<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>Try It Yourself in Our Interactive Python Shell:<\/p>\n<p> <iframe loading=\"lazy\" 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>\n<h2>Method: Iterate in For Loop with End Argument<\/h2>\n<p>If you&#8217;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>\n<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\nlst = [1, 2, 3, 4, 5] # Iterate over each element x\n# in the list and customize printing\nfor x in lst: # Use the end argument to define # what to print after each element print(str(x), end=' ')<\/pre>\n<p>The output is:<\/p>\n<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>\n<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>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Advantages<\/th>\n<th>Disadvantages<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Fully customizable<\/td>\n<td>Relatively slow<\/td>\n<\/tr>\n<tr>\n<td>Simple<\/td>\n<td>Less concise<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>Try It Yourself in Our Interactive Code Shell:<\/p>\n<p> <iframe loading=\"lazy\" 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>\n<p>Let&#8217;s overcome the disadvantage of the for loop of being less concise!<\/p>\n<h2 id=\"block-d62f54a1-4fa6-4203-82ef-149dc9786c9f\">Method: Unpacking With Separator Argument<\/h2>\n<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 &#8220;unpack&#8221; 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>\n<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\nlst = [1, 2, 3, 4, 5] # Use three underscores as separator\nprint(*lst, sep='___')\n# 1___2___3___4___5 # Use an arrow as separator\nprint(*lst, sep='-->')\n# 1-->2-->3-->4-->5<\/pre>\n<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>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Advantages<\/th>\n<th>Disadvantages<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Fully customizable<\/td>\n<td>Harder to read for beginners<\/td>\n<\/tr>\n<tr>\n<td>Fast<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Concise<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>Try It Yourself in Our Interactive Code Shell:<\/p>\n<p> <iframe loading=\"lazy\" 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>\n<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>\n<h2 id=\"block-d62f54a1-4fa6-4203-82ef-149dc9786c9f\">Method: Use the string.join() Method<\/h2>\n<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>\n<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\nlst = ['1', '2', '3', '4', '5'] # Use three underscores as separator\nprint('___'.join(lst))\n# 1___2___3___4___5 # Use arrow as separator\nprint('-->'.join(lst))\n# 1-->2-->3-->4-->5\n<\/pre>\n<p>Note that you can only use this methods if the list elements are already strings. If they are integers, joining them together doesn&#8217;t work and <a href=\"https:\/\/blog.finxter.com\/python-crash-course\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python <\/a>throws an error:<\/p>\n<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>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Advantages<\/th>\n<th>Disadvantages<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Fully customizable<\/td>\n<td>Harder to read for beginners<\/td>\n<\/tr>\n<tr>\n<td>Concise<\/td>\n<td>Slow<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>Works only for string elements<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<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>\n<p> <iframe loading=\"lazy\" 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>\n<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>\n<h2 id=\"block-d62f54a1-4fa6-4203-82ef-149dc9786c9f\">Method: Use the string.join() Method with Map()<\/h2>\n<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&#8217;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>\n<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\nlst = [1, 2, 3, 4, 5] # Use three underscores as separator\nprint('___'.join(map(str, lst)))\n# 1___2___3___4___5 # Use arrow as separator\nprint('-->'.join(map(str, lst)))\n# 1-->2-->3-->4-->5\n<\/pre>\n<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>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Advantages<\/th>\n<th>Disadvantages<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Fully customizable<\/td>\n<td>Harder to read for beginners<\/td>\n<\/tr>\n<tr>\n<td>Concise<\/td>\n<td>Slow<\/td>\n<\/tr>\n<tr>\n<td>Works for all data types<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>Try It Yourself in Our Interactive Code Shell:<\/p>\n<p> <iframe loading=\"lazy\" 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>\n<p>So, let&#8217;s finish this up!<\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<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>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;ll not only learn how to print lists in Python, you&#8217;ll become a better coder overall. If you just want to know the best way to print a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-111885","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/111885","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=111885"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/111885\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=111885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=111885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=111885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}