Two-dimensional arrays¶
Content summary
This lesson introduces two-dimensional arrays.
Problem statement¶
How do we handle data that is no longer just a simple list of numbers, but a table of values? For example:
- A class gradebook with multiple columns (subjects).
- A mathematical matrix.
- An image, which is essentially a grid of pixels.
In such cases, a one-dimensional array is no longer sufficient. We need a new structure that can represent data in rows and columns — this is called a two-dimensional array.
Concept¶
Two-dimensional array
A two-dimensional array is a data structure used to store elements in a table format with rows and columns.
Each element is identified by two indices:
- The row index
- The column index
Every row has the same number of elements (equal to the number of columns), and all rows are of equal length.
The image below illustrates a two-dimensional array A.
In other words, a two-dimensional array can be thought of as an array of arrays — each element in the main array is itself a one-dimensional array (a row).
Real-life examples of two-dimensional arrays:
- A chessboard
- A Sudoku grid
- An Excel spreadsheet
Two-dimensional arrays are ideal for working with:
- Tables
- Grids
- Matrices
Two-dimensional arrays in Python¶
As in the previous lesson, we will use the open-source library numpy to work with two-dimensional arrays.
Installing the numpy Library¶
Refer back to the installation instructions for the numpy library here.
Importing the Library¶
After installation, import the numpy library using the import statement. By convention, we use np as its short name.
Initialization¶
To create a two-dimensional array, we use the array() function from numpy.
The elements are listed inside nested square brackets: [[ ], [ ], ..., [ ]]
Example:
Line 5 creates the two-dimensional array A by explicitly listing its elements.
\nis the newline character.
Running the code above produces the following output:
To create a two-dimensional array where all elements have the same value, we use the full() function from numpy.
Example:
Line 13 creates an array called zeros with 3 rows and 4 columns, where every element is 0.
Running the code above produces the following output:
Accessing elements¶
Each element is accessed using its row index and column index, placed inside two pairs of square brackets [][], with the row index first and the column index second.
Example:
Lines 11 and 14 print:
- The first element of array
A - The element at row 2, column 3 (which is also the last element in this case)
Running the code above produces the following output:
Traversing the array¶
If we treat the two-dimensional array A as a list of rows, then each element of A is a one-dimensional array (a row).
- To get the number of rows, use
len(A). - To get the number of columns (i.e., the number of elements in each row), apply
len()to any row — usually the first one:len(A[0]).
Example:
Lines 8 and 12 get the number of rows and columns of array A and store them in the variables row and col.
Running the code above produces the following output:
When traversing a two-dimensional array, we usually use two nested loops:
- The outer loop iterates over the rows.
- The inner loop iterates over the columns (i.e., the elements of the current row).
Example:
Lines 16 to 23 use two nested for loops** to print the array A in a table-like format.
- Prints an extra space after each element.
Running the code above produces the following output:
Source code¶
The complete code is available at:
Summary mindmap¶
Some English words¶
| Vietnamese | Tiếng Anh |
|---|---|
| cột | column |
| hàng | row |
| mảng của mảng | array of arrays |
| mảng hai chiều | two-dimensional array |