NumPy Array Iterations (Python Tutorial)

NumPy Array Iterations

This is a detailed tutorial of the NumPy Iterating Arrays. Learn to perform iterations on a Numpy Array with the help of illustrative examples.

Iteration is the process of visiting each and every element of the array one by one. So when we try to iterate through the arrays, we use a for loop fo that purpose.

What is FOR loop?

It is a loop function that helps us to loop through a block of code for a particular number of times.

As in NumPy, we deal with multi-dimensional arrays sow can use this basic for loop which found in python.

Let us take some examples for different dimensional arrays and see how we can use this for a loop.

Example 1

In this case, we are considering a one-dimensional array:

#importing the numpy package and also making an alias as np 
import numpy as np 
# creating the array in 1-D 
array1=np.array([9,8,7,6,5,4,3,2,1]) 
# now we will apply for loop
for x in array1:
#printing the array 
  print(x)

Output.

9
8
7
6
5
4
3
2
1

Here it is going through each element of the array. As a result, it is printing each and every element it went through.

Example 2

In this example we will iterate through a two-dimensional array:

#importing the numpy package and also making an alias as np 
import numpy as np 
# creating the array in 2-D 
array1=np.array([[9,8,7,6,5,4],[3,2,1,0,1,2]]) 
# now we will use for loop to iterate 
for a in array1:
#printing the  array 
  print(a)

Output.

[9 8 7 6 5 4]
[3 2 1 0 1 2]

In this example, it will go through every row of the array, and as a result, it will keep printing them one by one.

In order to get the actual value of the array, we will have to iterate through each possible dimension.

#importing the numpy package and also making an alias as np 
import numpy as np 
# creating the array in 2-D 
array1=np.array([[9,8,7,6,5,4],[3,2,1,0,1,2]]) 
# now we will use for loop to iterate and get values 
for a in array1: 
 for b in a:
#printing the array 
   print(b)

Output.

9
8
7
6
5
4
3
2
1
0
1
2

So now we are getting the actual values.

Example 3

In this example, we will be taking a three-dimensional array to iterate through:

#importing the numpy package and also making an alias as np 
import numpy as np 
# creating the array in 3-D 
array1=np.array([[[9,8,7],[6,5,4]],[[3,2,1],[0,1,2]]]) 
# now we will apply for loop
for a in array1:
#printing the array 
   print(a)

Output.

[[9 8 7]
 [6 5 4]]
[[3 2 1]
 [0 1 2]]

In this case, it will iterate through every 2-D array.

In order to get all the scalar values, we will be giving the following commands:

#importing the numpy package and also making an alias as np 
import numpy as np # creating the array in 3-D 
array1=np.array([[[9,8,7],[6,5,4]],[[3,2,1],[0,1,2]]]) 
# now we will apply for loop on the array 
for a in array1: 
  for b in a: 
    for c in b: 
#printing the array 
        print(c)

Output.

9
8
7
6
5
4
3
2
1
0
1
2

Here we got all the scalars values. Similarly, we will get them whenever we iterate through any dimension.

Using nditer():

It is a helper function used to solve very simple to complex problems that occur during the process of iteration. This helps us to get the scalar values without applying various for loops.

Let us take an example:

#importing the numpy package and also making an alias as np 
import numpy as np 
# creating the array in 3-D 
array1=np.array([[[9,8,7],[6,5,4]],[[3,2,1],[0,1,2]]]) 
# now we will apply nditer on the array 
for a in np.nditer(array1):
#printing the array 
    print(a)

Output.

9
8
7
6
5
4
3
2
1
0
1
2

So we get all the scalar value of the array without applying multiple for loops.

Let us take another example for a 2-D array:

#importing the numpy package and also making an alias as np 
import numpy as np 
# creating the array in 2-D 
array1=np.array([[9,8,7,6,5,4],[3,2,1,0,1,2]]) 
# now we will use nditer to iterate 
for a in np.nditer(array1):
#printing the array 
      print(a)

Output.

9
8
7
6
5
4
3
2
1
0
1
2

So as a result w directly get the scalar values using this helper function.

We can also iterate through arrays by using different step sizes. In this, we can filter the array by skipping the necessary steps and then also iterating through the array at the same time.

let us go through an example:

#importing the numpy package and also making an alias as np 
import numpy as np 
# creating the array in 2-D 
array1=np.array([[9,8,7,6],[3,2,1,0]]) 
# now we will use for loop to iterate 
for a in np.nditer(array1[:, ::2]): 
#printing the array 
    print(a)

Output.

9
7
3
1

Here are getting values by leaving one value in between.

Enumerated Iteration

Enumeration is the action of mentioning the index of the elements one by one along with the elements themselves. we use ndenumerate() a method for getting the index of the element along with the element itself.

Let us go through an example for better understanding:

#importing the numpy package and also making an alias as np 
import numpy as np 
# creating the array in 1-D 
array1=np.array([9,8,7,6,5,4,3,2,1]) 
# now we will the method
for y,x in np.ndenumerate(array1): 
#printing the array 
    print(y,x)

Output.

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

Now here we are getting the index number of the elements along with the element itself.

Now let us take an example of a 2-D array:

#importing the numpy package and also making an alias as np 
import numpy as np 
# creating the array in 2-D 
array1=np.array([[9,8,7,6],[3,2,1,0]]) 
# now we will use the method 
for y,x in np.ndenumerate(array1): 
#printing the array 
    print(y,x)

Output.

(0, 0) 9
(0, 1) 8
(0, 2) 7
(0, 3) 6
(1, 0) 3
(1, 1) 2
(1, 2) 1
(1, 3) 0

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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