{"id":112295,"date":"2020-05-01T16:50:31","date_gmt":"2020-05-01T16:50:31","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=8073"},"modified":"2020-05-01T16:50:31","modified_gmt":"2020-05-01T16:50:31","slug":"how-to-print-a-list-of-list-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/05\/01\/how-to-print-a-list-of-list-in-python\/","title":{"rendered":"How to Print a List of List in Python?"},"content":{"rendered":"<p><strong>Short answer: To print a list of lists in Python without brackets and aligned columns, use the <code>''.join()<\/code> function and a generator expression to fill each string with enough whitespaces so that the columns align:<\/strong><\/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 list of lists\nlst = [['Alice', 'Data Scientist', '121000'], ['Bob', 'Java Dev', '99000'], ['Ann', 'Python Dev', '111000']] # Find maximal length of all elements in list\nn = max(len(x) for l in lst for x in l) # Print the rows\nfor row in lst: print(''.join(x.ljust(n + 2) for x in row))<\/pre>\n<p>I&#8217;ll explain this code (and simpler variants) in the following video:<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"How to Print a List of List in Python?\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/F1gddgWWaEI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p>As an exercise, you can also try it yourself in our interactive shell:<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/brython.info\/console.html\" width=\"800\" height=\"400\"><\/iframe> <\/p>\n<h2>Step-By-Step Problem Formulation<\/h2>\n<p>Do you want to print a <a href=\"https:\/\/blog.finxter.com\/python-list-of-lists\/\" target=\"_blank\" rel=\"noreferrer noopener\">list of lists<\/a> in Python so that the format doesn&#8217;t look like a total mess? In this <a href=\"https:\/\/blog.finxter.com\/python-crash-course\/\" target=\"_blank\" rel=\"noreferrer noopener\">tutorial<\/a>, I&#8217;ll show you how to orderly <a href=\"https:\/\/blog.finxter.com\/print-python-list\/\">print <\/a>a list of lists in Python&#8212;without using any external library.<\/p>\n<p>Before you start to think about the solution, you should understand the problem. What happens if you print a list of lists? Well, let&#8217;s try:<\/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 = [['Alice', 'Data Scientist', 121000], ['Bob', 'Java Dev', 99000], ['Ann', 'Python Dev', 111000]]\nprint(lst)<\/pre>\n<p>The output is not very nice:<\/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=\"\">[['Alice', 'Data Scientist', 121000], ['Bob', 'Java Dev', 99000], ['Ann', 'Python Dev', 111000]]<\/pre>\n<p>Instead, you&#8217;d would at least want to see a beautiful output like this one where each row has a separate line and the rows are aligned:<\/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=\"\">[['Alice', 'Data Scientist', 121000], ['Bob', 'Java Dev', 99000], ['Ann', 'Python Dev', 111000]]<\/pre>\n<p>And maybe, you even want to see an output that aligns the three columns as well like this one:<\/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=\"\">Alice Data Scientist 121000 Bob Java Dev 99000 Ann Python Dev 111000 <\/pre>\n<p>I&#8217;ll show you all those different ways of printing a list of lists next. So let&#8217;s start with the easiest one:<\/p>\n<h2>Print List of Lists With Newline<\/h2>\n<p><strong>Problem<\/strong>: Given a list of lists, print it one row per line.<\/p>\n<p><strong>Example<\/strong>: Consider the following example list:<\/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, 6]]<\/pre>\n<p>You want to print the list of lists with a newline character after each inner list:<\/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\n4 5 6<\/pre>\n<p><strong>Solution<\/strong>: Use a for loop and a simple print statement:<\/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, 6]] for x in lst: print(*x)\n<\/pre>\n<p>The output has the desired form:<\/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\n4 5 6<\/pre>\n<p><strong>Explanation<\/strong>: The <a href=\"https:\/\/blog.finxter.com\/what-is-asterisk-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">asterisk operator<\/a> &#8220;unpacks&#8221; all values in the inner list <code>x<\/code> into the print statement. You must know that the print statement also takes multiple inputs and prints them, whitespace-separated, to the shell.<\/p>\n<p><strong>Related articles<\/strong>:<\/p>\n<ul>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/what-is-asterisk-in-python\/\" target=\"_blank\">Unpacking Operator *<\/a><\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/print-python-list\/\" target=\"_blank\">How to Print a List Beautifully<\/a><\/li>\n<li><a href=\"https:\/\/blog.finxter.com\/python-list-of-lists\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python Lists of Lists<\/a><\/li>\n<\/ul>\n<h2>Print List of Lists Without Brackets<\/h2>\n<p>To print a list of lists without brackets, use the following code again.<\/p>\n<p><strong>Solution<\/strong>: Use a for loop and a simple print statement:<\/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, 6]] for x in lst: print(*x)\n<\/pre>\n<p>The output has the desired form:<\/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\n4 5 6<\/pre>\n<p>But how can you print a list of lists by aligning the columns?<\/p>\n<h2>Print List of Lists Align Columns<\/h2>\n<p><strong>Problem<\/strong>: How to print a list of lists so that the columns are aligned?<\/p>\n<p><strong>Example<\/strong>: Say, you&#8217;re going to print the list of lists. <\/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=\"\">[['Alice', 'Data Scientist', 121000], ['Bob', 'Java Dev', 99000], ['Ann', 'Python Dev', 111000]]<\/pre>\n<p>How to align the columns?<\/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=\"\">Alice 'Data Scientist', 121000], Bob 'Java Dev', 99000], Ann 'Python Dev', 111000]]<\/pre>\n<p><strong>Solution<\/strong>: Use the following code snippet to print the list of lists and align all columns (no matter how many characters each string in the list of lists occupies). <\/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 list of lists\nlst = [['Alice', 'Data Scientist', '121000'], ['Bob', 'Java Dev', '99000'], ['Ann', 'Python Dev', '111000']] # Find maximal length of all elements in list\nn = max(len(x) for l in lst for x in l) # Print the rows\nfor row in lst: print(''.join(x.ljust(n + 2) for x in row))<\/pre>\n<p>The output is the desired:<\/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=\"\">Alice Data Scientist 121000 Bob Java Dev 99000 Ann Python Dev 111000 <\/pre>\n<p><strong>Explanation:<\/strong> <\/p>\n<ul>\n<li>First, you determine the length <code>n<\/code> (in characters) of the largest string in the list of lists using the statement <code>max(len(x) for l in lst for x in l)<\/code>. The code uses a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-does-nested-list-comprehension-work-in-python\/\" target=\"_blank\">nested for loop in a generator expression<\/a> to achieve this.<\/li>\n<li>Second, you iterate over each list in the list of lists (called <code>row<\/code>). <\/li>\n<li>Third, you create a string representation with columns aligned by &#8216;padding&#8217; each row element so that it occupies <code>n+2<\/code> characters of space. The missing characters are filled with empty spaces.<\/li>\n<\/ul>\n<p>You can see the code in action in the following memory visualizer. Just click &#8220;Next&#8221; to see which objects are created in memory if you run the code in Python:<\/p>\n<p> <iframe loading=\"lazy\" width=\"800\" height=\"500\" frameborder=\"0\" src=\"https:\/\/pythontutor.com\/iframe-embed.html#code=%23%20Create%20the%20list%20of%20lists%0Alst%20%3D%20%5B%5B'Alice',%20'Data%20Scientist',%20'121000'%5D,%0A%20%20%20%20%20%20%20%5B'Bob',%20'Java%20Dev',%20'99000'%5D,%0A%20%20%20%20%20%20%20%5B'Ann',%20'Python%20Dev',%20'111000'%5D%5D%0A%0A%0A%23%20Find%20maximal%20length%20of%20all%20elements%20in%20list%0An%20%3D%20max%28len%28x%29%20for%20l%20in%20lst%20for%20x%20in%20l%29%0A%0A%23%20Print%20the%20rows%0Afor%20row%20in%20lst%3A%0A%20%20%20%20print%28''.join%28x.ljust%28n%20%2B%202%29%20for%20x%20in%20row%29%29&#038;codeDivHeight=400&#038;codeDivWidth=350&#038;cumulative=false&#038;curInstr=0&#038;heapPrimitives=nevernest&#038;origin=opt-frontend.js&#038;py=3&#038;rawInputLstJSON=%5B%5D&#038;textReferences=false\"> <\/iframe> <\/p>\n<p><strong>Related articles<\/strong>: You may need to refresh your understanding of the following Python features used in the code:<\/p>\n<ul>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\">List comprehension and generator expressions<\/a><\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/print-python-list\/\" target=\"_blank\">String join()<\/a><\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/print-python-list\/\" target=\"_blank\">Printing lists<\/a><\/li>\n<\/ul>\n<h2>Print List of Lists with Pandas<\/h2>\n<p>Last but not least, I&#8217;ll show you my simple favorite: simply import the pandas library (the excel of Python coders) and print the DataFrame. Pandas takes care of pretty formatting:<\/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=\"\">\nlst = [['Alice', 'Data Scientist', '121000'], ['Bob', 'Java Dev', '99000'], ['Ann', 'Python Dev', '111000']] import pandas as pd\ndf = pd.DataFrame(lst)\nprint(df)\n<\/pre>\n<p>The output looks beautiful&#8212;like a spreadsheet in your Python shell:<\/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=\"\"> 0 1 2\n0 Alice Data Scientist 121000\n1 Bob Java Dev 99000\n2 Ann Python Dev 111000<\/pre>\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>Short answer: To print a list of lists in Python without brackets and aligned columns, use the &#8221;.join() function and a generator expression to fill each string with enough whitespaces so that the columns align: # Create the list of lists lst = [[&#8216;Alice&#8217;, &#8216;Data Scientist&#8217;, &#8216;121000&#8217;], [&#8216;Bob&#8217;, &#8216;Java Dev&#8217;, &#8216;99000&#8217;], [&#8216;Ann&#8217;, &#8216;Python Dev&#8217;, &#8216;111000&#8217;]] [&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-112295","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\/112295","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=112295"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/112295\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=112295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=112295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=112295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}