How to Enumerate Elements of 1-D NumPy Array


In this example we will show how to enumerate the elements of the 1-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,), value: 1
index: (1,), value: 2
index: (2,), value: 3
index: (3,), value: 4
index: (4,), value: 5
index: (5,), value: 6
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments