0
0
NumPydata~10 mins

2D array indexing (row, col) in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - 2D array indexing (row, col)
Create 2D array
Choose row index
Choose column index
Access element at (row, col)
Return element value
We start with a 2D array, pick a row and column index, then access the element at that position.
Execution Sample
NumPy
import numpy as np
arr = np.array([[10,20,30],[40,50,60],[70,80,90]])
element = arr[1,2]
print(element)
This code creates a 3x3 array and accesses the element in the 2nd row, 3rd column.
Execution Table
StepActionRow IndexColumn IndexElement AccessedOutput
1Create 2D array---[[10 20 30] [40 50 60] [70 80 90]]
2Select row index1---
3Select column index12--
4Access element at (1,2)12arr[1,2]60
5Print element---60
💡 Element at row 1, column 2 is accessed and printed; execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 4Final
arrundefined[[10 20 30] [40 50 60] [70 80 90]][[10 20 30] [40 50 60] [70 80 90]][[10 20 30] [40 50 60] [70 80 90]]
elementundefinedundefined6060
row indexundefinedundefined11
column indexundefinedundefined22
Key Moments - 3 Insights
Why do we use arr[1,2] instead of arr[1][2]?
In numpy, arr[1,2] accesses the element at row 1, column 2 directly in one step, which is more efficient. arr[1][2] works but accesses row 1 first as a sub-array, then column 2, which is two steps.
What happens if the row or column index is out of range?
An IndexError occurs because numpy arrays have fixed sizes. For example, arr[3,0] is invalid since row 3 does not exist (rows are 0 to 2). See execution_table step 4 for valid indexing.
Are row and column indices zero-based or one-based?
Indices start at 0 in numpy. So row 1 means the second row, column 2 means the third column. This is shown in the execution_table where arr[1,2] accesses the second row, third column.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'element' after step 4?
A50
B60
C30
D40
💡 Hint
Check the 'Element Accessed' and 'Output' columns in step 4 of the execution_table.
At which step is the 2D array 'arr' created?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in the execution_table for when the array is first defined.
If we change the row index to 0 and column index to 1, what element will be accessed?
A40
B50
C20
D10
💡 Hint
Refer to the variable_tracker for arr values and remember indexing starts at 0.
Concept Snapshot
2D array indexing syntax: arr[row, col]
Indices start at 0 (zero-based).
Access element by specifying row then column.
Out-of-range indices cause errors.
arr[1,2] accesses 2nd row, 3rd column element.
Use arr[row, col] for efficient access.
Full Transcript
This visual trace shows how to access elements in a 2D numpy array using row and column indices. We create a 3x3 array, then pick row 1 and column 2 to get the element 60. Indices start at zero, so row 1 is the second row. The execution table tracks each step: array creation, index selection, element access, and printing. The variable tracker shows how 'arr' and 'element' change. Key moments clarify why numpy uses arr[row, col] syntax, zero-based indexing, and what happens if indices are out of range. The quiz tests understanding of element values and indexing steps.