# Iteration Of NumPy Array

we will explore the various methods to iterate over a NumPy array and understand their characteristics, advantages, and limitations. Whether you are a beginner or an experienced NumPy user.

## NumPy Array Iteration

NumPy arrays are a fundamental data structure in the Python scientific computing stack, used for representing and manipulating large amounts of numerical data.

Iterating over the elements of a NumPy array is a common task that can be performed in several ways.

Numpy array items iteration means iterating each item one at a time.

In numpy, we can utilize the simple for loop of Python to manipulate multidimensional arrays.

An array of one dimensional items will be iterated one by one if we iterate on it.

The below code imports the numpy library and creates a numpy array company_arr with the values [1, 2, 3]. Then, it iterates over the array using a for loop and prints each value in the array, mrx.

#### Example:

import numpy as npy comapny_arr = npy.array(["MICROSOFT", "ORACLE", "SAP", "IBM", "ADOBE"]) for mrx in comapny_arr: print(mrx)

Utilizing the following cgpa_arr array as a starting point, iterate through its components:

#### Example:

import numpy as npy cgpa_arr = npy.array([3.41, 2.69, 3.82, 394, 4]) for mrx in cgpa_arr: print(mrx)

## NumPy Array Iteration 2-D

Numpy array iteration loops through each row in a two-dimensional array.

Utilizing the below code, we create a 2D numpy array called company_arr with two sub-arrays, each containing a list of company names. After that, it uses a for loop to iterate over the 2D array and print each sub-array, mrx.

#### Example:

import numpy as npy company_arr = npy.array([["MICROSOFT", "ORACLE", "SAP", "IBM"], ["SAP SE", "Fiserv", "ADP", "Broadcom"]]) for mrx in company_arr: print(mrx)

Applying the two-dimensional array, traverse the data as follows:

#### Example:

import numpy as npy nba_arr = npy.array([["Toronto Raptors", "Chicago Bulls", "Cleveland Cavaliers", "Detroit Pistons"], ["Philadelphia 76ers", "New York Knicks", "Boston Celtics", "Miami Heat"]]) for mrx in nba_arr: print(mrx)

In Numpy array iteration, if we iterate over an n-D array, we traverse n-1 dimensions sequentially.

In every dimension, we are required to iterate through the arrays to get the real values, the scalars.

There are two lists in the array, each containing four elements. These lists are referred to as “mrx” in the first “for” loop.

The second “for” loop prints each element within each list, which are referred to as “ample“:

#### Example:

import numpy as npy company_arr = npy.array([["MICROSOFT", "ORACLE", "SAP", "IBM"], ["SAP SE", "Fiserv", "ADP", "Broadcom"]]) for mrx in company_arr: for ample in mrx: print(ample)

For every two-dimensional scalar object, run the following iterations:

#### Example:

import numpy as npy nba_arr = npy.array([["Toronto Raptors", "Chicago Bulls", "Cleveland Cavaliers", "Detroit Pistons"], ["Philadelphia 76ers", "New York Knicks", "Boston Celtics", "Miami Heat"]]) for mrx in nba_arr: for ample in mrx: print(ample)

## Iterating 3-D Arrays

Numpy array iteration in a three-dimensional array has to go over all two-dimensional arrays.

Utilizing the following cgpa_arr three-dimensional array, loop through the items:

#### Example:

import numpy as npy cgpa_arr = npy.array([[[2.33, 2.48, 2.66, 2.83], [3.15, 3.24, 3.33, 3.45]], [[3.5, 3.58, 3.63, 3.67], [3.7, 3.74, 3.89, 3.99]]]) for mrx in cgpa_arr: print(mrx)

You can traverse the components in the following three-dimensional array by calling nba_arr:

#### Example:

import numpy as npy nba_arr = npy.array([[["Toronto Raptors", "Chicago Bulls", "Cleveland Cavaliers", "Detroit Pistons"], ["Philadelphia 76ers", "New York Knicks", "Boston Celtics", "Miami Heat"]], [["LA Clippers", "Los Angeles Lakers", "Phoenix Suns", "New Orleans Pelicans"], ["Memphis Grizzlies", "Dallas Mavericks", "Houston Rockets", "San Antonio Spurs"]]]) for mrx in nba_arr: print(mrx)

In every dimension, we need to loop through the arrays to retrieve the true values, the scalars.

To reach the scalars, loop through as follows:

#### Example:

import numpy as npy even_arr = npy.array([[[0, 2, 4, 6], [8, 10, 12, 14]], [[16, 18, 20, 22], [24, 26, 28, 30]]]) for mrx1 in even_arr: for mrx2 in mrx1: for mrx3 in mrx2: print(mrx3)

To get the scalars, traverse below:

#### Example:

import numpy as npy odd_arr = npy.array([[[1, 3, 5, 7], [9, 11, 13, 15]], [[17, 19, 21, 23], [25, 27, 29, 31]]]) for ample1 in odd_arr: for ample2 in ample1: for ample3 in ample2: print(ample3)

## NumPy Array Iteration Using nditer()

A helping function, nditer(), can be invoked for a wide range of iterations, from the very beginning to the very advanced.

Let’s take a look at Numpy array Iteration examples to see how it solves a few simple iteration problems.

For high-dimensional arrays, you must apply n for loops to iterate through every scalar in a simple for loop.

Repeat the below watch_arr three-dimensional array multiple times:

#### Example:

import numpy as npy watch_arr = npy.array([[["Cartier Santos", "Jaeger-LeCoultre Reverso"], ["IWC Pilot's Watch", "Patek Philippe Perpetual Calendar Chronograph"]], [["Rolex Datejust", "Breitling Navitimer"], ["Rolex Submariner", "Omega Speedmaster"]]]) for mrx in npy.nditer(watch_arr): print(mrx)

Execute iterations on the below singer_arr three-dimensional array:

#### Example:

import numpy as npy singer_arr = npy.array([[["Ariana Grande", "Michael Jackson"], ["Taylor Swift", "XXXTentacion"]], [["Eminem", "Elvis Presley"], ["Tupac Shakur", "Lady Gaga"]]]) for mrx in npy.nditer(singer_arr): print(mrx)

## NumPy Array Iteration Data Types

The op_dtypes argument can be utilized to modify the object datatypes while iterating over the expected datatype.

Since NumPy does not modify the data type of the item in-place (in arrays), it requires some extra space for this, which is called a buffer, which is enabled by flags=[‘buffered’] in nditer().

A string representation of looping through the factorial_arr array is as follows:

#### Example:

import numpy as npy factorial_arr = npy.array([1, 2, 6, 24, 120]) for mrx in npy.nditer(factorial_arr, flags=['buffered'], op_dtypes=['S']): print(mrx)

The given string indicates traversing the fibonacci_arr array:

#### Example:

import numpy as npy fibonacci_arr = npy.array([0, 1, 1, 2, 3, 5, 8]) for mrx in npy.nditer(fibonacci_arr, flags=['buffered'], op_dtypes=['S']): print(mrx)

## NumPy Array Iteration Step Size

We can apply filtering and then proceed with the iteration process.

Simply skip one scalar item as you loop through the singer_arr two-dimensional array:

#### Example:

import numpy as npy singer_arr = npy.array([["Ariana Grande", "Michael Jackson", "Taylor Swift", "XXXTentacion"], ["Eminem", "Elvis Presley", "Tupac Shakur", "Lady Gaga"]]) for mrx in npy.nditer(singer_arr[:, ::3]): print(mrx)

While traversing the fibonacci_arr array, skip one scalar component:

#### Example:

import numpy as npy fibonacci_arr = npy.array([[0, 1, 1, 2, 3], [5, 8, 13, 21, 34]]) for mrx in npy.nditer(fibonacci_arr[1:, ::3]): print(mrx)

## NumPy Array Iteration ndenumerate()

According to its sequence, enumeration refers to listing something’s order number.

The ndenumerate() method can be utilized when we require the associated index of an item while iterating.

List the components of the following company_arr one-dimensional arrays:

#### Example:

import numpy as npy company_arr = npy.array(["MICROSOFT", "ORACLE", "SAP", "IBM", "ADOBE"]) for mrx, ample in npy.ndenumerate(company_arr): print(mrx, ample)

The below factorial_arr one-dimensional array items need to be enumerated:

#### Example:

import numpy as npy factorial_arr = npy.array([0, 1, 2, 6, 24, 120]) for mrx, ample in npy.ndenumerate(factorial_arr): print(mrx, ample)

In the below example, we will enumerate the components of the following nba_arr two-dimensional array:

#### Example:

import numpy as npy nba_arr = npy.array([["Toronto Raptors", "Chicago Bulls", "Cleveland Cavaliers", "Detroit Pistons"], ["Philadelphia 76ers", "New York Knicks", "Boston Celtics", "Miami Heat"]]) for mrx, ample in npy.ndenumerate(nba_arr): print(mrx, ample)

Following is an enumeration of the items of a watch_arr two-dimensional array:

#### Example:

import numpy as npy watch_arr = npy.array(["Cartier Santos", "Jaeger-LeCoultre Reverso", "IWC Pilot's Watch", "Patek Philippe Perpetual Calendar Chronograph", "Rolex Datejust", "Breitling Navitimer", "Rolex Submariner", "Omega Speedmaster"]) for mrx, ample in npy.ndenumerate(watch_arr): print(mrx, ample)
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0