how to iterate element of n-dimensional array in simple ways

Iteration is a general term for taking each item of something, one after another. Any time you use a loop, explicit or implicit, to go over a group of items, that is iteration. NumPy array iteration is indispensable for a wide range of data manipulation tasks due to its efficiency and versatility.
 
NumPy array iteration is crucial for various data manipulation tasks. we will explore the basic and more powerful iteration capabilities of NumPy n-dimensional arrays, So fasten your seatbelt, sharpen your curiosity, and let’s embark on this exhilarating journey into the heart of NumPy n-dimensional array iteration!
 
Numpy provides several mechanisms for performing iterations over n-dimensional array by using the following ways.

  1. By using python’s loop.
  2. By using numpy.nditer() function.
  3. By using numpy.ndenumerate() function.
1. By using python loop
    If we iterate n-dimensional array by using python’s for loop, we will get 1-D array.
 
#Iterate element of 1-D array
import numpy as np 
a=np.array([10,20,30,40,50])
for i in a:
  print(i)

o/p-
10
20
30
40
50

#Iterate elements of 2-D array
a=np.array([[10,20],[30,40],[50,60]]) print('print element one by one') for x in a: for j in x: print(j) o/p- print element one by one 10 20 30 40 50 60

#Iterate element of 3-D array
a=np.arange(1,13).reshape(2,3,2)

print('Print element of 3-D array one by one')
for i in a: #2-D array
  for j in i: #1-D array
    for k in j: #scaler value
      print(k)
o/p-
Print element of 3-D array one by one
1
2
3
4
5
6
7
8
9
10
11
12

 

Note
To iterate element of n-D array by using python ,we required n loop ,which is not convenient to overcome this problem we should go for numpy.nditer() function

2. By using numpy.nditer() function
  1. This function is specially designed function to iterate elements of any n-D array easily without multiple loops.
  2. Before changing the data type we required temporary storage ,which is nothing but buffer. We have to enable that buffer other wise we get error.
#Iterate the element of 2-D array

a=np.array([[10,20],[30,40],[50,60]])
for i in np.nditer(a):
  print(i)

o/p-
10
20
30
40
50
60

#Iterate elements of 3-D array.
a=np.arange(1,9).reshape(2,2,2)
for i in np.nditer(a):
  print(i)

o/p-
1
2
3
4
5
6
7
8

#Change the data type of element using op_dtypes parameter.
a=np.arange(1,9).reshape(2,2,2)
for i in np.nditer(a,flags=['buffered'],op_dtypes=['float']):
  print(i)

o/p-
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
 
3. By Using numpy.ndenumerate() function.   
  1.  By using nditer() function we will get elements only but not indexes.
  2. If we want index also in addition to element then we should use numpy.ndenumerate() function.
  3. numpy.ndenumerate() function returns multidimensional index iterator which yield pairs of array coordinates and values.
#Iterate elements of 1-D array
a=np.arange(5)
for key,value in np.ndenumerate(a):
  print(f'{value} present at index {key}')

o/p-
0 present at index (0,)
1 present at index (1,)
2 present at index (2,)
3 present at index (3,)
4 present at index (4,)

#Iterate elements of 2-D array
a=np.array([[10,20],[30,40],[50,60]])
for key,value in np.ndenumerate(a):
  print(f'{value} present at index {key}')

o/p-
10 present at index (0, 0)
20 present at index (0, 1)
30 present at index (1, 0)
40 present at index (1, 1)
50 present at index (2, 0)
60 present at index (2, 1)

 

Before we wrap up this post i.e how to iterate element of n-dimensional array in simple ways, I want to take a moment to extend my heartfelt thanks to all of you who have read this blog. We’re eager to hear what you think about  this blog post.

Whether you have a comment, a question, or a personal story related to this topic, we encourage you to share it with us in the comments section below. Your voice matters, and we’re excited to engage in a meaningful conversation with you.

Thank You!! Happy Learning!!

You May Also Like

About the Author: Nitesh

I am a software engineer and Enthusiastic to learn new things

Leave a Reply

Your email address will not be published. Required fields are marked *