NumPy Tutorial NumPy Statistics NumPy Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

NumPy - Iterating over Arrays



NumPy contains an iterator object numpy.nditer, which is an efficient multi-dimensional iterator object to iterate over arrays. It provides many flexible ways to iterate over all elements of one or more arrays in a systematic way.

Single Array Iteration

import numpy as np

Arr = np.array([0, 30, 60, 90])
#converting the angles in radians
Arr = Arr*np.pi/180

print("\nThe sin value of angles:")
print(np.sin(Arr))

print("\nThe cos value of angles:")
print(np.cos(Arr))

print("\nThe tan value of angles:")
print(np.tan(Arr))

The output of the above code will be:

The sin value of angles:
[ 0.         0.5        0.8660254  1.       ]

The cos value of angles:
[  1.00000000e+00   8.66025404e-01   5.00000000e-01   6.12323400e-17]

The tan value of angles:
[  0.00000000e+00   5.77350269e-01   1.73205081e+00   1.63312394e+16]

5