How to Print a List Without Truncating?
Per default, Python doesn’t truncate lists when printing them to the shell, even if they are large. For example, you can call
print(my_list) and see the full list even if the list has one thousand elements or more!
Here’s an example:
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.
Here’s an example:
How to Print a NumPy Array Without Truncating?
In many cases, large NumPy arrays when printed out are not truncated as well on the default Python programming environment IDLE:
However, in the interactive mode of the Python shell, a NumPy array may be truncated, unlike a Python list:
>>> np.arange(10000) array([ 0, 1, 2, ..., 9997, 9998, 9999])
To print the NumPy array without truncating, simply
>>> import sys, numpy >>> numpy.set_printoptions(threshold=sys.maxsize) >>> np.arange(10000)
The output shows the full array without converting it to a list first:
Not all output is shown to save some space. 🙂
Of course, you could also convert the NumPy array to a Python list first.
👉 Recommended Tutorial: How to Print a NumPy Array Without Truncating It?
Feel free to check out our Python cheat sheets and free email academy:
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.