Iterating Arrays
nditer and ndenumerate visit every element. Prefer vectorised operations.
Example
a = np.arange(4).reshape(2, 2)
for idx, v in np.ndenumerate(a):
print(idx, v)
print([x for x in a.flat])Result
(0, 0) 0 (0, 1) 1 (1, 0) 2 (1, 1) 3 [np.int64(0), np.int64(1), np.int64(2), np.int64(3)]
Exercise
Index and value.
np.(a)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.