Skip to content

Latest commit

 

History

History
34 lines (26 loc) · 856 Bytes

ArrayIterating.md

File metadata and controls

34 lines (26 loc) · 856 Bytes

Array Iteration in Python

  • Iterating means going through elements one by one.
  • While dealing with multi-dimensional arrays in Numpy, we can perform the basic "for" loop of Python.

Iterating 1D Array

import numpy as np
arr = np.array([1,2,3])
for x in arr:
print(x)


In a 1D array, iteration goes through each element one by one.

Iterating 2D Array

ab = np.array([[1,2,3], [4,5,6]])
for x in ab:
print(x)


In a 2D array, iteration goes through all of the rows.

Iterating 3D Array

abc = np.array([[[1,2,3],[4,5,6]], [[7,8,9],[10,11,12]]])
for x in abc:
print(x)


In a 3D array, iteration will go through all 2D arrays.

NOTE: If we iterate on an n-D array, it'll go through (n-1)th dimension one by one.