0
0
NumPydata~5 mins

2D array indexing (row, col) in NumPy

Choose your learning style9 modes available
Introduction

We use 2D array indexing to find or change values in a table-like structure by pointing to its row and column.

Looking up a specific value in a spreadsheet-like data.
Changing a pixel color in an image represented as a 2D array.
Extracting a row or column from a dataset for analysis.
Updating a cell in a game board like tic-tac-toe.
Accessing sensor data arranged by time and sensor number.
Syntax
NumPy
import numpy as np

# Create a 2D array
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [7, 8, 9]])

# Access element at row i, column j
value = array_2d[i, j]

# Change element at row i, column j
array_2d[i, j] = new_value

Rows and columns start counting from 0, so the first row is 0, the first column is 0.

Use comma inside the square brackets to separate row and column indices.

Examples
Accessing the top-left element (row 0, column 0).
NumPy
import numpy as np

array_2d = np.array([[10, 20, 30],
                     [40, 50, 60],
                     [70, 80, 90]])

# Access element at first row, first column
print(array_2d[0, 0])  # Output: 10
Edge case: array with only one element.
NumPy
import numpy as np

array_2d = np.array([[5]])

# Access the only element in a 1x1 array
print(array_2d[0, 0])  # Output: 5
Edge case: empty 2D array has no elements to access.
NumPy
import numpy as np

array_2d = np.array([]).reshape(0, 0)

# Trying to access element in empty array will cause error
# print(array_2d[0, 0])  # This will raise IndexError
Changing the bottom-right element (row 2, column 2) to 99.
NumPy
import numpy as np

array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [7, 8, 9]])

# Change element at last row, last column
array_2d[2, 2] = 99
print(array_2d)
Sample Program

This program creates a 3x3 array, prints it, accesses one element, then changes another element and prints the updated array.

NumPy
import numpy as np

# Create a 3x3 2D array
array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [7, 8, 9]])

print("Original array:")
print(array_2d)

# Access element at row 1, column 2
value = array_2d[1, 2]
print(f"Value at row 1, column 2: {value}")

# Change element at row 0, column 1
array_2d[0, 1] = 20
print("Array after changing element at row 0, column 1 to 20:")
print(array_2d)
OutputSuccess
Important Notes

Time complexity for accessing or changing one element is O(1) - very fast.

Space complexity depends on the array size, but indexing itself uses no extra space.

Common mistake: forgetting that indexing starts at 0, so row 1 is actually the second row.

Use 2D indexing when you want to work with specific cells; use slicing if you want whole rows or columns.

Summary

2D array indexing uses [row, column] to find or change values.

Rows and columns start at 0, so count carefully.

Accessing or changing one element is very fast and simple.