How to Enumerate Elements of 2-D NumPy Array


In this example we will show how to enumerate the elements of the 2-D NumPy array using the ndenumerate() method in Python.

Source Code

import numpy as np

n = np.array([[1, 2, 3], [4, 5, 6]])

for id_x, x in np.ndenumerate(n):
    print('index: {}, value: {}'.format(id_x, x))

Output:

index: (0, 0), value: 1
index: (0, 1), value: 2
index: (0, 2), value: 3
index: (1, 0), value: 4
index: (1, 1), value: 5
index: (1, 2), value: 6
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments