Array indexing is used to access, retrieve, or modify individual elements stored in an array. Each element is identified by its position, known as an index, which allows direct access to the required value.
Accessing Elements with Indexes
Indexing is the most common way to retrieve values from an array. Each element has a position called an index, which allows you to access it directly without traversing the entire array.
import array
arr = array.array('i', [10, 20, 30, 40, 50])
print(arr[0])
print(arr[1])
print(arr[4])
Output
10 20 50
Explanation:
- arr[0] accesses the element at index 0, which is 10.
- arr[1] accesses the element at index 1, which is 20.
- arr[4] accesses the element at index 4, which is 50.
- Python arrays use zero-based indexing, so counting starts from 0.
Negative Indexing
Negative indexing allows you to access elements from the end of an array. It is useful when you need the last few elements and do not want to calculate the array length.
import array
arr = array.array('i', [10, 20, 30, 40, 50])
print(arr[-1])
print(arr[-2])
Output
50 40
Explanation:
- arr[-1] returns the last element of the array.
- arr[-2] returns the second-last element.
- Negative indexes start from the end, where -1 represents the last position.
Indexing in 2D Arrays
In a 2D array, data is organized in rows and columns. To access a specific value, you need to specify both the row index and the column index.
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print(matrix[0][0])
print(matrix[1][2])
print(matrix[2][1])
Output
1 6 8
Explanation:
- matrix[0][0] accesses the first element of the first row.
- matrix[1][2] accesses the third element of the second row.
- matrix[2][1] accesses the second element of the third row.
- The first index selects the row, while the second index selects the column.
Indexing in 3D Arrays
A 3D array contains multiple 2D arrays. Accessing an element requires three indexes: one for the layer, one for the row, and one for the column.
arr = [[[1, 2, 3], [4, 5, 6]],
[[10, 11, 12], [13, 14, 15]]]
print(arr[0][0][0])
print(arr[1][1][1])
print(arr[1][0][2])
Output
1 14 12
Explanation:
- arr[0][0][0] accesses the first element of the first row in the first layer.
- arr[1][1][1] accesses the second element of the second row in the second layer.
- arr[1][0][2] accesses the third element of the first row in the second layer.
- Each additional dimension requires one more index.
Modifying Elements Using Indexes
Indexes can also be used to update existing values in an array.
import array
arr = array.array('i', [10, 20, 30, 40])
arr[2] = 100
print(arr)
Output
array('i', [10, 20, 100, 40])
Explanation:
- arr[2] refers to the element at index 2.
- Assigning a new value replaces the existing value.
- Only the specified element is modified.
IndexError in Arrays
Accessing an index that does not exist raises an IndexError.
import array
arr = array.array('i', [10, 20, 30])
print(arr[5])
Output
ERROR!
Traceback (most recent call last):
File "<main.py>", line 3, in <module>
IndexError: array index out of range
Explanation:
- The array contains only three elements.
- Valid indexes are 0, 1 and 2.
- Accessing index 5 causes Python to raise an IndexError.
Array Slicing
Slicing allows you to extract multiple elements from an array using a range of indexes. It is useful when working with subsets of data.
import array
arr = array.array('i', [10, 20, 30, 40, 50, 60, 70])
print(arr[1:5])
print(arr[::2])
print(arr[-4:-1])
Output
array('i', [20, 30, 40, 50])
array('i', [10, 30, 50, 70])
array('i', [40, 50, 60])
Explanation:
- arr[1:5] returns elements from index 1 to 4.
- arr[::2] returns every second element from the array.
- arr[-4:-1] returns elements from the fourth-last position up to (but not including) the last position.
- Slicing follows the format array[start:end:step].