How To Iterate on a Numpy Array

How To Iterate on an Numpy Array

Iterating in Numpy array means is going through the elements of the array elements one by one.

If we want to iterate over a Multi Dimensional Array In Python then we can simply do this by using the for loop in Python.

How To Iterate Over an 1-D Array In Numpy Python

import numpy as np

arr = np.array([4, 5, 6])

for x in arr:
  print(x)

Output:

4,5,6

How To Iterate Over a 2-D Array In Numpy Python

At the time of iterating over a 2-D Array it will iterate through all the rows in 2-D Array

import numpy as np

arr = np.array([[4, 5, 6], [7, 8, 9]])

for x in arr:

  print(x)

Output:

[4 5 6]
[7 8 9]

How To Iterate On A 3-D Array In Numpy Python

For a 3-D Array It will go through all the lements of the 2-D array

import numpy as np

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

for x in arr:
  print("x represents the 2-D array:")
  print(x)


Output:

x represents the 2-D array:
[[1 2 3]
 [4 5 6]]
x represents the 2-D array:
[[ 7  8  9]
 [10 11 12]]

How to Get The Actual Values In Scalar Format By Iterating A 3-D Array In Numpy Python


To return the actual value from 3-D Array we actually have to iterate over each dimension of the array

import numpy as np

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

for x in arr:
  for y in x:
    for z in y:
      print(z)

Output:

1
2
3
4
5
6
7
8
9
10
11
12

How To Iterate Array By Using nditer() Function In Numpy

The function ndtier() is a function that is mainly used to do the very basic to very advanced level iterations

With this function some very basic level issue can be solved.

How To Iterater on Each Scalar Element of the Array

In basic for loops, iterating through each scalar of an array we need to use n for loops which can be difficult to write for arrays with very high dimensionality.

Example:

import numpy as np

arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

for x in np.nditer(arr):
  print(x)

Output:

1
2
3
4
5
6
7
8

How To Iterate Numpy Array With Different Data Types



We can use op_dtypes argument and can pass it to the expected datatype to change the datatype of the array elements while iterating the array.

NumPy does not change the data type of the element in-place (where the element is in array) so it needs some other space to perform this action, that extra space is called buffer, and in order to enable it in nditer() we pass flags=['buffered'].

Numpy does not basically change the data type of the element in-place and it requires some other space to perform this action.This extra space is called buffer and to enable this functionality in ndtier() we pass the flags=['buffered']

Let's see an exmaple below

How To Iterate through the array as a string:


import numpy as np

arr = np.array([1, 2, 3])

for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']):
  print(x)

Output:

b'1'

b'2'

b'3


How To Iterate Over a Numpy Array With Different Size

In this below example we will iterate and print the every alternate element

import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

for x in np.nditer(arr[:, ::2]):
  print(x)

Output:

1
3
5
7

How To Do Enumerated Iteration Using ndenumerate() function for Numpy Array In Python

Enumerate on following 1D arrays elements:


import numpy as np

arr = np.array([1, 2, 3])

for idx, x in np.ndenumerate(arr):
  print(idx, x)

Output:


(0,) 1
(1,) 2
(2,) 3

Enumerate on following 2D array's elements:

import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

for idx, x in np.ndenumerate(arr):
  print(idx, x)

Output:

(0, 0) 1
(0, 1) 2
(0, 2) 3
(0, 3) 4
(1, 0) 5
(1, 1) 6
(1, 2) 7
(1, 3) 8




Comments