Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Print List Without Truncating

#1
Python Print List Without Truncating

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;760193&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2>How to Print a List Without Truncating?</h2>
<p class="has-global-color-8-background-color has-background">Per default, Python doesn’t truncate lists when printing them to the shell, even if they are large. For example, you can call <code>print(my_list)</code> and see the full list even if the list has one thousand elements or more! </p>
<p>Here’s an example:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="758" height="101" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-43.png" alt="" class="wp-image-760206" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-43.png 758w, https://blog.finxter.com/wp-content/uplo...300x40.png 300w" sizes="(max-width: 758px) 100vw, 758px" /></figure>
</div>
<p>However, Python may squeeze the text (e.g., in programming environments such as IDLE) so you would have to press the button before seeing the output. The reason is that showing the whole output could be time-consuming and visually cluttering.</p>
<p>Here’s an example:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="722" height="73" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-44.png" alt="" class="wp-image-760214" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-44.png 722w, https://blog.finxter.com/wp-content/uplo...300x30.png 300w" sizes="(max-width: 722px) 100vw, 722px" /></figure>
</div>
<h2>How to Print a NumPy Array Without Truncating?</h2>
<p>In many cases, large NumPy arrays when printed out are not truncated as well on the default Python programming environment IDLE:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-45.png" alt="" class="wp-image-760221" width="701" height="78" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-45.png 784w, https://blog.finxter.com/wp-content/uplo...300x34.png 300w, https://blog.finxter.com/wp-content/uplo...768x86.png 768w" sizes="(max-width: 701px) 100vw, 701px" /></figure>
</div>
<p>However, in the interactive mode of the Python shell, a NumPy array may be truncated, unlike a Python list:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> np.arange(10000)
array([ 0, 1, 2, ..., 9997, 9998, 9999])</pre>
<p>To print the NumPy array without truncating, simply</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> import sys, numpy
>>> numpy.set_printoptions(threshold=sys.maxsize)
>>> np.arange(10000)</pre>
<p>The output shows the full array without converting it to a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332" target="_blank">list</a> first:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="561" height="97" src="https://blog.finxter.com/wp-content/uploads/2022/10/image-46.png" alt="" class="wp-image-760236" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/image-46.png 561w, https://blog.finxter.com/wp-content/uplo...300x52.png 300w" sizes="(max-width: 561px) 100vw, 561px" /></figure>
</div>
<p>Not all output is shown to save some space. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>Of course, you could also convert the NumPy array to a Python list first. </p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended Tutorial</strong>: <a href="https://blog.finxter.com/how-to-print-the-full-numpy-array-without-truncation/" data-type="post" data-id="34452" target="_blank" rel="noreferrer noopener">How to Print a NumPy Array Without Truncating It?</a></p>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<p>Feel free to check out our Python cheat sheets and free email academy:</p>
</div>


https://www.sickgaming.net/blog/2022/10/...runcating/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016