Write a Python Program to Print Numpy Array Items. We can use the print statement to print the numpy array items.
import numpy as np
arr = np.array([15, 20, 40, 78, 50, 99, 248, 122])
print("*** Numpy Array Items ***")
print(arr)
strarr = np.array(['India', 'USA', 'Cananda', 'Japan'])
print("\n*** Numpy Array Items ***")
print(strarr)
Printing the Numpy Array items output
*** Numpy Array Items ***
[ 15 20 40 78 50 99 248 122]
*** Numpy Array Items ***
['India' 'USA' 'Cananda' 'Japan']
We can use the asterisk (*) operator before the array name to unpack the array elements. It means the print() statement takes one array element as an argument and prints it.
import numpy as np
a = np.array([15, 20, 40, 78, 50, 99, 248, 122])
print(*a)
print(*a, sep = " - ")
15 - 20 - 40 - 78 - 50 - 99 - 248 – 122
If the task is to convert or print numpy array items as a list, we can use the built-in tolist() method.
import numpy as np
a = np.array([15, 20, 40, 78, 50])
print(a.tolist())
[15, 20, 40, 78, 50]
Python Program to Print Numpy Array Items using the For Loop
The for loop (for num in arr) iterates from the first numpy array item to the last array item, and the print statement prints the array items.
import numpy as np
arr = np.array([15, 20, 40, 78, 50, 99, 248, 122])
print("*** Numpy Array Items ***")
for num in arr:
print(num)
print("\n*** Numpy Array Items ***")
for num in arr:
print(num, end = " ")
Print the Numpy Array items using a for loop output
*** Numpy Array Items ***
15
20
40
78
50
99
248
122
*** Numpy Array Items ***
15 20 40 78 50 99 248 122
In this Python example, we used the for loop range (or i in range(len(arr))) to iterate over the numpy array using the index positions. The print statement prints the numpy array item at each index position.
import numpy as np
arr = np.array([12, 20, 50, 18, 33, 50, 99])
print("*** Numpy Array Items ***")
for i in range(len(arr)):
print("Array Item at %d Position = %d" %(i, arr[i]))

Program to print 2D array items
This program uses the * operator and the flat method to display or print all elements in the two-dimensional array (also called a flat array).
import numpy as np
a = np.array([[15, 20, 40], [78, 50, 90]])
print(*a.flat)
15 20 40 78 50 90
Also Read