How to Iterate over a NumPy Array

Problem Formulation and Solution Overview

In this article, you’ll learn how to iterate over a 1D, 2D and 3D NumPy array using Python.

When working with the NumPy library, you will encounter situations where you will need to iterate through a 1D, 2D and even a 3D array. This article will show you how to accomplish this task.


πŸ’¬ Question: How would we write code to iterate through a 1D, 2D or 3D NumPy array?

We can accomplish this task by one of the following options:


Preparation

Before moving forward, please ensure the NumPy library is installed. Click here if you require instructions.

Then, add the following code to the top of each script. This snippet will allow the code in this article to run error-free.

import numpy as np 

After importing the NumPy library, this library is referenced by calling the shortcode (np).


Method 1: Use a For Loop and np.array()

This method uses a For loop combined with np.array() to iterate through a 1D NumPy array. The first five (5) Atomic Numbers from the Periodic Table are generated and displayed for this example.

atomic_els = np.array(np.arange(1,6))

for el in atomic_els:
    print(el, end=' ')

Above, calls the np.array() function and passes it np.arange(1,6) with two (2) arguments: a start position of one (1) and stop position of five (5) or (stop-1). The result is an array of integers and saves to atomic_els.

[1 2 3 4 5]

Next, a For loop is instantiated and iterates through each element of the 1D NumPy array atomic_els.

The output is sent to the terminal on one (1) line as an additional argument was passed to the print statement (end=' '). This argument replaces the default newline character with a blank space.

1 2 3 4 5

Method 2: Uses a For Loop and nditer()

This method uses a for loop combined with np.nditer() to iterate through a NumPy array. The first five (5) Atomic Numbers and Number of Neutrons data from the Periodic Table display for this example.

atomic_data = np.array([np.arange(1,6), [0, 2, 4, 5, 6]])

for dim in atomic_data:
    for d in dim:
        print(d, end=' ')

Above, calls the np.array() function and passes np.arange(1,6) as the first argument and an array of associated Number of Neutrons values as the second.

Next, an outer for loop is instantiated. This iterates through each array dimension.

Inside this loop, another for loop is instantiated. The inside loop iterates through each element of the above dimensions.

The output is sent to the terminal on one (1) line as an additional argument is passed to the print statement (end=' '). This argument replaces the default newline character with a blank space.

1 2 3 4 5 0 2 4 5 6

πŸ’‘Note: The first dimension is highlighted to easily differentiate the dimensions.


Method 3: Use a For Loop and itertools

This method uses a for loop and Python’s built-in itertools library to iterate through a NumPy array. The first three (3) Atomic Numbers, Phase, and Group data from the Periodic Table display for this example.

import itertools 
  
atomic_num = np.arange(1,4)
atomic_phase = ['gas', 'gas', 'solid']
atomic_group = [1, 18, 1]
  
for (a, b, c) in itertools.zip_longest(atomic_num, atomic_phase, atomic_group, fillvalue=0):
    print (a, b, c)

Above, imports the itertools library to use the zip_longest() function.

Then, three (3) arrays are declared containing the relevant data for the first three (3) elements from the Periodic Table. They save respectively to atomic_num, atomic_phase, and atomic_group.

Next, a for loop is instantiated and passed three (3) arguments inside the brackets (a, b, c) which reference the arguments inside the zip_longest(atomic_num, atomic_phase, atomic_group) function.

An additional argument (fillvalue=0) is also passed. This fills in any missing values for lists of unequal lengths with the stated value.

The loop iterates until the end of the arrays are reached, outputting to the terminal for each iteration.

1 gas 1
2 gas 18
3 solid 1

Method 4: Use a While Loop and Size

This method uses a while loop combined with np.size to iterate through a NumPy array. The first five (5) Atomic Numbers, Number of Neutrons, and Phase data from the Periodic Table display for this example.

atomic_names = np.array(['Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'Boron'])
el_count = 0

while el_count < atomic_names.size:
    print(atomic_names[el_count], end=' ')
    el_count += 1

Above, calls the np.array() function and passes a list containing the first five (5) element names of the Periodic Table. These results save to atomic_names.

Then, a variable, el_count, is declared with a start value of zero (0). This will be a counter variable and lets the While loop know when to stop iterating.

Next, a while loop is instantiated and iterates through the elements of atomic_names. This will continue while the value of el_count remains less than the value of atomic_names.size.

The output is sent to the terminal on one (1) line as an additional argument is passed to the print statement (end=' '). This argument replaces the default newline character with a blank space.

Hydrogen Helium Lithium Beryllium Boron

Method 5: Use a For Loop and np.ndenumerate()

This method uses a For loop and np.ndenumerate() to iterate through a NumPy array. The first five (5) Element Names from the Periodic Table display for this example.

atomic_names = np.array(['Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'Boron'])

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

Above, calls the np.array() function and passes it an array containing the first five (5) element names from the Periodic Table. The results save to atomic_names.

Next, a for loop is instantiated referencing idx which is the index of the array, and x, which is the array element’s value.

The output is sent to the terminal. Then idx displays a Tuple containing the index value of the array and then the value of x for each iteration.

(0,) Hydrogen
(1,) Helium
(2,) Lithium
(3,) Beryllium
(4,) Boron

Method 6: Use a For Loop and range()

This method uses a for loop and range()) to iterate through a 3D NumPy array.

nums = np.array([[[1, 2],   [3, 4],   [5, 6]],
                  [[7, 8],   [9, 10],  [11, 12]],
                  [[13, 14], [15, 16], [17, 18]],
                  [[19, 20], [21, 22], [23, 23]],
                  [[24, 25], [26, 27], [28, 29]]])

for x in range(0, 5):
    for y in range(0, 3):
        for z in range(0, 2):
            print(nums[x][y][z])

Above declares a 3D NumPy array containing consecutive numbers from 1-29 inclusive. These save to nums.

Next, three (3) for loops are instantiated to loop through and output the contents of nums to the terminal one (1) number per line.

The range() function for each loop is based on the dimensions of the 3D Numpy array, for example:

  • The first loop (range(0,5) identifies the total number of tows in the array.
  • The next loop (range(0,3) identifies the number of columns in the array.
  • The final loop (range(0,2) identifies the elements in each column in the array.

Finally, the output is sent to the terminal (snippet only).

1
2
3
4
5
6
7
8
9
10
...

Bonus: Convert CSV to np.array()

This example reads in a snippet of the Periodic Table as a CSV file. This data is converted to a NumPy array and is output to the terminal.

The CSV file below contains the values of the Atomic Number, Atomic Mass, Number of Neutrons, and the Number of Electrons for the first seven (7) elements from the Periodic Table.

Contents of pt_sample.csv

1,1.007,0
2,4.002,2
3,6.941,4
4,9.012,5
5,10.811,6
6,12.011,6
7,14.007,7

To follow along, save this file as pt_sample.csv and move it into the current working directory.

Before moving forward, please ensure the Pandas library is installed. Click here if you require instructions.

import pandas as pd
import csv

file_name = 'pt_sample.csv'
df_data = np.array(list(csv.reader(open(file_name, 'r'), delimiter=','))).astype("float")
print(df_data)

Above, imports the Pandas library and the CSV library. This is needed to work with DataFrames and read in the CSV file.

Then, a filename is declared and saves to file_name.

On the highlighted line, the np.array() function is called and passed the following arguments.

  • The csv.reader() function is passed as an argument and this argument passes the open() method to open the specified CSV file in read (r) mode, including the field delimiter (csv.reader(open(file_name, 'r'), delimiter=',')).
  • The contents of the CSV file converts to a list.
  • This list converts to an np.array().

The results save to df_data and output to the terminal.

[[ 1. 1.007 0. ]
[ 2. 4.002 2. ]
[ 3. 6.941 4. ]
[ 4. 9.012 5. ]
[ 5. 10.811 6. ]
[ 6. 12.011 6. ]
[ 7. 14.007 7. ]]

Summary

These seven (7) methods of iterating a NumPy array should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!


Programmer Humor – Blockchain

“Blockchains are like grappling hooks, in that it’s extremely cool when you encounter a problem for which they’re the right solution, but it happens way too rarely in real life.” source xkcd