IKH

A B C chapter 6

Iterate elements of nd array

  • Iteration means getting all elements one by one.

We can iterate nd arrays in 3 ways

  • By using Python’s loops concept.
  • By using nditer() function.
  • By using ndenumerate() function.

By using Python’s loops concept

Iterate elemetns of 1-D array

Example

Python
import numpy as np
a = np.arange(10,51,10)
for x in a:
print(x)

Output

PowerShell
10
20
30
40
50

Iterate elemetns of 2-D array

Example

Python
import numpy as np
a = np.array([[10,20,30],[40,50,60],[70,80,90]])
for x in a: #x is 1-D array but not scalar value
for y in x: # y is scalar value present in 1-D array
print(y)

Output

PowerShell
10
20
30
40
50
60
70
80
90

Iterate elemetns of 3-D array

Example

Python
import numpy as np
a = np.array([[[10,20],[30,40]],[[50,60],[70,80]]])
for x in a: #x is 2-D array but not scalar value
for y in x: # y is 1-D array but not scalar value
for z in y: # z is scalar value
print(z)

Output

PowerShell
10
20
30
40
50
60
70
80

Note

To iterate elements of n-D array, we require n loops.

By using nditer() function

Advantage: To iterate any n-D array only one loop is enough.

  • nditer is a class present in numpy library.
  • nditer() ==> Creating an object of nditer class.

Iterate elements of 1-D array

Example

Python
import numpy as np
a = np.arange(10,51,10)
for x in np.nditer(a):
print(x)

Output

PowerShell
10
20
30
40
50

Iterate elements of 2-D array

Example

Python
import numpy as np
a = np.array([[10,20,30],[40,50,60],[70,80,90]])
for x in np.nditer(a):
print(x)

Output

PowerShell
10
20
30
40
50
60
70
80
90

Iterate elements of 3-D array

Example

Python
import numpy as np
a = np.array([[[10,20],[30,40]],[[50,60],[70,80]]])
for x in np.nditer(a):
print(x)

Output

PowerShell
10
20
30
40
50
60
70
80

Iterate elements of sliced array also

Example

Python
import numpy as np
a = np.array([[10,20,30],[40,50,60],[70,80,90]])
for x in np.nditer(a[:,:2]):
print(x)

Output

PowerShell
10
20
40
50
70
80

Using nditer() to get elements of required data type

  • We have to use op_dtypes parameter.

Example

Python
import numpy as np
help(np.nditer)

Output

PowerShell
Help on class nditer in module numpy:
class nditer(builtins.object)
| nditer(op, flags=None, op_flags=None, op_dtypes=None, order='K', casting=
'safe', op_axes=None, itershape=None, buffersize=0)

Example

Python
import numpy as np
a = np.array([[10,20,30],[40,50,60],[70,80,90]])
for x in np.nditer(a,op_dtypes=['float']):
print(x)
print(a)

Output

PowerShell
---------------------------------------------------------------------------
TypeError
Traceback (most recent call last)
<ipython-input-182-64f49c1b6d2f> in <module>
1 import numpy as np
2 a = np.array([[10,20,30],[40,50,60],[70,80,90]])
----> 3 for x in np.nditer(a,op_dtypes=['float']):
4    print(x)
5 print(a)
TypeError: Iterator operand required copying or buffering, but neither copyin
g nor buffering was enabled

Example

Python
# Numpy won't change the type of elements in existing array.
# To store changed type elements, we required temporary storage, which is nothing
but buffer.
# We have to enable that buffer.
import numpy as np
a = np.array([[10,20,30],[40,50,60],[70,80,90]])
for x in np.nditer(a,flags=['buffered'],op_dtypes=['float']):
print(x)
print(a)

Output

PowerShell
10.0
20.0
30.0
40.0
50.0
60.0
70.0
80.0
90.0
[[10 20 30]
 [40 50 60]
 [70 80 90]]

Example

Python
import numpy as np
a = np.array([[[10,20],[30,40]],[[40,50],[60,70]]])
for x in np.nditer(a):
print(x.dtype) #int32

Output

PowerShell
int32
int32
int32
int32
int32
int32
int32
int32

Example

Python
for x in np.nditer(a,flags=['buffered'],op_dtypes=['int64']):
print(x.dtype) #int64

Output

PowerShell
int64
int64
int64
int64
int64
int64
int64
int64

Normal Python’s loops vs nditer()

Python Loops

  • n loops are required.
  • There is no way to specify our requ.

nditer()

  • only one loop is enough.
  • There is a way to specify our required dtype. For this we have to use op_dtypes argument.

By using ndenumerate() function

  • By using nditer() we will get elements only but not indexes.
  • If we want indexes also in addition to elements, then we should use ndenumerate() function.
  • ndenumerate() function returns multidimensional index iterator which yields pairs of array indexes(coordinates) and values.

Iterate elements of 1-D array

Example

Python
# Iterate elements of 1-D array:
import numpy as np
a = np.array([10,20,30,40,50,60,70])
for pos,element in np.ndenumerate(a):
  print(f'{element} element present at index/position:{pos}')

Output

PowerShell
10  element  present  at index/pos it Ion: (0,)
20  element  present  at index/pos it Ion: (1,)
30  element  present  at index/pos it Ion: (2,)
40  element  present  at index/pos it Ion: (3,)
50  element  present  at index/pos it Ion: (4,)
60  element  present  at index/pos it Ion: (5,)
70  element  present  at index/pos it Ion: (6,)

Iterate elements of 2-D array

Example

Python
# Iterate elements of 2-D array:
import numpy as np
a = np.array([[10,20,30],[40,50,60],[70,80,90]])
for pos,element in np.ndenumerate(a):
print(f'{element} element present at index/position:{pos}')

Output

PowerShell
10  element  present  at  index/position:(0, 0)
20  element  present  at  index/position:(0, 1)
30  element  present  at  index/position:(0, 2)
40  element  present  at  index/position:(1, 0)
50  element  present  at  index/position:(1, 1)
60  element  present  at  index/position:(1, 2)
70  element  present  at  index/position:(2, 0)
80  element  present  at  index/position:(2, 1)
90  element  present  at  index/position:(2, 2)

Iterate elements of 3-D array:

Example

Python
# Iterate elements of 3-D array:
import numpy as np
a = np.arange(1,25).reshape(2,3,4)
for pos,element in np.ndenumerate(a):
print(f'{element} element present at index/position:{pos}')

Output

PowerShell
element  present  at  index/position:(0,0,0)
element  present  at  index/position:(0,0,1)
element  present  at  index/position:(0,0,2)
element  present  at  index/position:(0,0,3)
element  present  at  index/position:(0,1,0)
element  present  at  index/position:(0,1,1)
element  present  at  index/position:(0,1,2)
element  present  at  index/position:(0,1,3)
element  present  at  index/position:(0,2,0)
element  present  at  index/position:(0,2,1)
element  present  at  index/position:(0,2,2)
element  present  at  index/position:(0,2,3)
element  present  at  index/position:(1,0,0)
element  present  at  index/position:(1,0,1)
element  present  at  index/position:(1,0,2)
element  present  at  index/position:(1,0,3)
element  present  at  index/position:(1,1,0)
element  present  at  index/position:(1,1,1)
element  present  at  index/position:(1,1,2)
element  present  at  index/position:(1,1,3)
element  present  at  index/position:(1,2,0)
element  present  at  index/position:(1,2,1)
element  present  at  index/position:(1,2,2)
element  present  at  index/position:(1,2,3)

Report an error