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.
import numpy as np arr = np.array([[10,20,30],[40,50,60],[70,80,90]]) element = arr[1,2] print(element)
| Step | Action | Row Index | Column Index | Element Accessed | Output |
|---|---|---|---|---|---|
| 1 | Create 2D array | - | - | - | [[10 20 30] [40 50 60] [70 80 90]] |
| 2 | Select row index | 1 | - | - | - |
| 3 | Select column index | 1 | 2 | - | - |
| 4 | Access element at (1,2) | 1 | 2 | arr[1,2] | 60 |
| 5 | Print element | - | - | - | 60 |
| Variable | Start | After Step 1 | After Step 4 | Final |
|---|---|---|---|---|
| arr | undefined | [[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]] |
| element | undefined | undefined | 60 | 60 |
| row index | undefined | undefined | 1 | 1 |
| column index | undefined | undefined | 2 | 2 |
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.
arr[2, 1] mean when arr is a 2D numpy array?arr[2, 1] accesses the element at row 3, column 2.arr?arr[0, 2] accesses the element in the 1st row and 3rd column.import numpy as np arr = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]]) print(arr[1, 2])
arr[1, 2], so the output is 30.import numpy as np arr = np.array([[1, 2], [3, 4]]) print(arr[2, 0])
arr[2, 0] causes an IndexError because row 2 does not exist.arr filled with numbers 1 to 16 in row-major order. Which code correctly changes the element in the last row and second column to 100?arr[3, 1] which is correct. arr[4, 2] = 100 uses invalid index 4. arr[-1, 2] = 100 uses column index 2 (third column), not second. arr[3, -2] = 100 uses negative index -2 for column which is the third column, not second.Pass the test and this pattern moves unknown