Skip to content
Related Articles

Related Articles

Print full Numpy array without truncation
  • Last Updated : 11 Oct, 2020

Numpy is the fundamental library of python, used to perform scientific computing. It provides high-performance multidimensional arrays and tools to deal with them. A NumPy array is a grid of values (of the same type) that are indexed by a tuple of positive integers. Numpy arrays are fast, easy to understand and give users the right to perform calculations across entire arrays.

Let us print number from 0 to 1000 by using simple NumPy functions

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import numpy as np
arr = np.arange(1001)
print(arr)

chevron_right


The output will be displayed like this 

[   0    1    2 ...  998  999 1000]

Whenever we have to print a large range of numbers then the output will be truncated and the result will be displayed within one line. But what to do if we don’t want truncation of output.



numpy.set_printoptions()

In NumPy, it is possible to remove truncation and display results as it is. We use numpy.set_printoptions() function having attribute threshold = np.inf or threshold = sys.maxsize

Syntax: numpy.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None,
suppress=None, nanstr=None, infstr=None, formatter=None)
 

Example 1: using threshold = sys.maxsize

Printing first 1000 numbers without truncation using np.set_printoptions(threshold = sys.maxsize)

Here, we set the threshold to sys.maxsize which represents the maximum values that python can handle.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing Numpy and sys modules
import numpy as np
import sys
  
# Creating a 1-D array with 100 values
arr = np.arange(101)
  
# Printing all values of array without truncation
np.set_printoptions(threshold=sys.maxsize)
print(arr)

chevron_right


Output:

[  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
 18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35
 36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53
 54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71
 72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89
 90  91  92  93  94  95  96  97  98  99 100]



The above example shows how we can print a large range of values(like 0 to 100) without truncation.

Example 2: using threshold = np.inf

Printing first 1200 numbers without truncation using np.set_printoptions(threshold = np.inf)

Here, we set the threshold to np.inf which is the floating-point representation of infinity.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing Numpy and sys modules
import numpy as np
import sys
  
# Creating a 1-D array with 50 values
arr = np.arange(51)
  
# Printing all values of array without truncation
np.set_printoptions(threshold=np.inf)
print(arr)

chevron_right


Output:

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49 50]

The above example shows how we can print a large range of values(like 0 to 50) without truncation.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :