{"id":4427,"date":"2025-05-04T19:53:57","date_gmt":"2025-05-04T19:53:57","guid":{"rendered":"https:\/\/www.askpython.com\/?p=4427"},"modified":"2025-10-24T17:56:17","modified_gmt":"2025-10-24T17:56:17","slug":"print-an-array-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/array\/print-an-array-in-python","title":{"rendered":"How to Print Arrays in Python?"},"content":{"rendered":"\n<figure class=\"wp-block-pullquote\"><blockquote><p>To print arrays in Python, you can use the print() function directly for simple output or implement loops for formatted display. This guide covers both approaches for 1D and 2D arrays with practical examples.<\/p><\/blockquote><\/figure>\n\n\n\n<p>Array printing in Python hits different when you&#8217;re debugging at 3 AM.<\/p>\n\n\n\n<p>What starts as a simple <a href=\"https:\/\/www.askpython.com\/python\/built-in-methods\/python-print-function\" data-type=\"post\" data-id=\"2517\">print() statement<\/a> can turn into an entire formatting philosophy.<\/p>\n\n\n\n<p>I&#8217;ve been down this rabbit hole. Multiple times.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Are Python Arrays?<\/h2>\n\n\n\n<p><strong>First things first, there are no arrays in Python<\/strong>. <\/p>\n\n\n\n<p>You get lists and <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\" data-type=\"post\" data-id=\"1070\">NumPy arrays<\/a>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Lists are Python&#8217;s default<\/strong> &#8211; flexible, forgiving, and deceptively simple. <\/li>\n\n\n\n<li><strong>NumPy arrays want type consistency<\/strong>. They reward you with performance.<\/li>\n<\/ul>\n\n\n\n<p>This distinction becomes critical when you&#8217;re trying to make sense of your output at 2 AM.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Printing Python Arrays Using Lists<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Directly Printing Arrays with print()<\/h3>\n\n\n\n<p>Sometimes the simplest approach is the right one:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Simple 1D array\nnumbers = &#x5B;2, 4, 5, 7, 9]\nprint(&quot;Array:&quot;, numbers)\n\n# 2D array (list of lists)\nmatrix = &#x5B;&#x5B;1, 2], &#x5B;3, 4]]\nprint(&quot;2D Array:&quot;, matrix)\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nArray: &#x5B;2, 4, 5, 7, 9]\n2D Array: &#x5B;&#x5B;1, 2], &#x5B;3, 4]]\n\n<\/pre><\/div>\n\n\n<p>Quick. Dirty. Perfect for those &#8220;what the hell is in this variable?&#8221; moments.<\/p>\n\n\n\n<p>But we&#8217;re not always debugging. Sometimes we need our data to actually make sense to humans.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Custom Formatted Printing with Loops<\/h3>\n\n\n\n<p>This is where you separate the juniors from the seniors. The ability to format output properly:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# 1D array with custom spacing\ndata = &#x5B;2, 4, 5, 7, 9]\nprint(&quot;Values: &quot;, end=&quot;&quot;)\nfor value in data:\n    print(value, end=&quot; &quot;)\nprint()  # New line\n\n# 2D array as a grid\ngrid = &#x5B;&#x5B;1, 2, 3], &#x5B;4, 5, 6], &#x5B;7, 8, 9]]\nprint(&quot;Grid format:&quot;)\nfor row in grid:\n    for item in row:\n        print(f&quot;{item:2}&quot;, end=&quot; &quot;)  # Format with width 2\n    print()  # New line after each row\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nValues: 2 4 5 7 9 \nGrid format:\n 1  2  3 \n 4  5  6 \n 7  8  9 \n\n<\/pre><\/div>\n\n\n<p>Now we&#8217;re talking. Readable output. Aligned columns. The kind of formatting that won&#8217;t make your eyes bleed during code review.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Printing Python NumPy Arrays<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Basic NumPy Array Printing<\/h3>\n\n\n\n<p>NumPy doesn&#8217;t mess around with formatting:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# 1D NumPy array\nvector = np.array(&#x5B;1, 2, 3, 4])\nprint(&quot;Vector:&quot;, vector)\n\n# 2D NumPy array\nmatrix = np.array(&#x5B;&#x5B;21, 43], &#x5B;22, 55], &#x5B;53, 86]])\nprint(&quot;Matrix:\\n&quot;, matrix)\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nVector: &#x5B;1 2 3 4]\nMatrix:\n &#x5B;&#x5B;21 43]\n &#x5B;22 55]\n &#x5B;53 86]]\n\n<\/pre><\/div>\n\n\n<p>Notice the difference? No commas. Automatic alignment. NumPy gets it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced NumPy Formatting and Printing<\/h3>\n\n\n\n<p>NumPy&#8217;s formatting options are&#8230; extensive. Sometimes overwhelmingly so:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# Control decimal places\nfloats = np.array(&#x5B;3.14159, 2.71828, 1.41421])\nnp.set_printoptions(precision=3)\nprint(&quot;Formatted floats:&quot;, floats)\n\n# Large arrays with ellipsis\nlarge_array = np.arange(100)\nnp.set_printoptions(threshold=10)\nprint(&quot;Large array:&quot;, large_array)\n\n# Reset to default\nnp.set_printoptions(edgeitems=3, threshold=1000, precision=8)\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nFormatted floats: &#x5B;3.142 2.718 1.414]\nLarge array: &#x5B; 0  1  2 ... 97 98 99]\n\n<\/pre><\/div>\n\n\n<p>Powerful when you need it. Overkill when you don&#8217;t.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">When Your Output Actually Matters<\/h3>\n\n\n\n<p>Let&#8217;s face it &#8211; most of us aren&#8217;t printing arrays for fun. We&#8217;re trying to make sense of data:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# Sales data by region and quarter\nsales_data = np.array(&#x5B;\n    &#x5B;45000, 52000, 48000, 51000],  # North\n    &#x5B;38000, 41000, 43000, 46000],  # South\n    &#x5B;55000, 58000, 54000, 59000],  # East\n    &#x5B;42000, 44000, 47000, 49000]   # West\n])\n\nregions = &#x5B;&#039;North&#039;, &#039;South&#039;, &#039;East&#039;, &#039;West&#039;]\nquarters = &#x5B;&#039;Q1&#039;, &#039;Q2&#039;, &#039;Q3&#039;, &#039;Q4&#039;]\n\n# Print formatted table\nprint(&quot;Sales by Region and Quarter&quot;)\nprint(&quot;-&quot; * 40)\nprint(&quot;Region  &quot;, end=&quot;&quot;)\nfor q in quarters:\n    print(f&quot;{q:&gt;8}&quot;, end=&quot;&quot;)\nprint()\nprint(&quot;-&quot; * 40)\n\nfor i, region in enumerate(regions):\n    print(f&quot;{region:&lt;8}&quot;, end=&quot;&quot;)\n    for sale in sales_data&#x5B;i]:\n        print(f&quot;{sale:8,}&quot;, end=&quot;&quot;)\n    print()\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nSales by Region and Quarter\n----------------------------------------\nRegion        Q1      Q2      Q3      Q4\n----------------------------------------\nNorth     45,000  52,000  48,000  51,000\nSouth     38,000  41,000  43,000  46,000\nEast      55,000  58,000  54,000  59,000\nWest      42,000  44,000  47,000  49,000\n\n<\/pre><\/div>\n\n\n<p>This is what clean output looks like.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Scientific Computing Array Printing<\/h3>\n\n\n\n<p>When precision matters more than aesthetics. Let&#8217;s display a <a href=\"https:\/\/www.askpython.com\/python\/examples\/correlation-matrix-in-python\" data-type=\"post\" data-id=\"8530\">correlation matrix<\/a>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# Correlation matrix\ncorrelation = np.array(&#x5B;\n    &#x5B;1.000, 0.812, 0.543],\n    &#x5B;0.812, 1.000, 0.721],\n    &#x5B;0.543, 0.721, 1.000]\n])\n\nvariables = &#x5B;&#039;Temperature&#039;, &#039;Pressure&#039;, &#039;Volume&#039;]\n\nprint(&quot;Correlation Matrix&quot;)\nprint(&quot;-&quot; * 50)\nprint(f&quot;{&#039;&#039;:12}&quot;, end=&quot;&quot;)\nfor var in variables:\n    print(f&quot;{var&#x5B;:11]:&gt;12}&quot;, end=&quot;&quot;)\nprint()\n\nfor i, var in enumerate(variables):\n    print(f&quot;{var&#x5B;:11]:&lt;12}&quot;, end=&quot;&quot;)\n    for j, value in enumerate(correlation&#x5B;i]):\n        print(f&quot;{value:12.3f}&quot;, end=&quot;&quot;)\n    print()\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nCorrelation Matrix\n--------------------------------------------------\n            Temperature    Pressure      Volume\nTemperature       1.000       0.812       0.543\nPressure          0.812       1.000       0.721\nVolume            0.543       0.721       1.000\n\n<\/pre><\/div>\n\n\n<p>Clean. Professional. The kind of output that doesn&#8217;t make your colleagues question your competence.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices and Reality Checks<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Choosing the Right Array Printing Method<\/h3>\n\n\n\n<p>Your approach should fit your context:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Debugging? print() and move on<\/li>\n\n\n\n<li>Code review coming up? Format that shit properly<\/li>\n\n\n\n<li>Scientific paper? NumPy formatting is your friend<\/li>\n\n\n\n<li>Teaching others? Clear examples with clean output<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Array printing in Python isn&#8217;t hard. But the difference between amateur hour and professional output is in these details.<\/p>\n\n\n\n<p>Start simple. Use print() when debugging. Format properly when sharing code. Use NumPy when you need precision.<\/p>\n\n\n\n<p>The best approach? Whatever makes your output understandable to the poor soul reading it later.<\/p>\n\n\n\n<p>Because that poor soul is usually future you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To print arrays in Python, you can use the print() function directly for simple output or implement loops for formatted display. This guide covers both approaches for 1D and 2D arrays with practical examples. Array printing in Python hits different when you&#8217;re debugging at 3 AM. What starts as a simple print() statement can turn [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":64325,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-4427","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-array"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=4427"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4427\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/64325"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=4427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=4427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=4427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}