NumPy Array Indexing and Slicing

Accessing and slicing array elements.

15 min read
Beginner

NumPy Array Indexing and Slicing

NumPy arrays can be indexed and sliced similar to that of Python Lists. In this lesson, we will be understanding these NumPy array operations in detail.

NumPy Array Indexing

Indexing refers to the way elements are accessed in a NumPy array.

The indexing of elements in a NumPy Array starts at 0 and you can access any element of an n-dimensional array by using the index number of the elements. You’ll understand this better with the help of the following examples of 1D and 2D NumPy arrays.

Accessing elements of a 1D NumPy Array

python
# Importing the NumPy library as np
import numpy as np

# Creating a NumPy array from a Python List
num_array = np.array([1, 2, 3])

# Printing the content
print("The content of the NumPy array: ", num_array)

# Printing the first element by accessing index 0
print("The first element of the NumPy array: ", num_array[0])

# Printing the second element by accessing index 1
print("The second element of the NumPy array: ", num_array[1])

# Printing the third element by accessing index 2
print("The third element of the NumPy array: ", num_array[2])
num_array=[123]\text{num\_array} = \begin{bmatrix} 1 & 2 & 3 \end{bmatrix}

You can also use negative indexing to access the elements of a 1D NumPy Array.

python
# Printing the content
print("The content of the NumPy array: ", num_array)

# Printing the first element by accessing index -3
print("The first element of the NumPy array: ", num_array[-3])

# Printing the second element by accessing index -2
print("The second element of the NumPy array: ", num_array[-2])

# Printing the third element by accessing index -1
print("The thrird element of the NumPy array: ", num_array[-1])

Accessing elements of a 2D NumPy Array

python
# Creating a NumPy array from a Python List
num_array = np.array([[1, 2, 3], [4, 5, 6]])

# Printing the content
print("The content of the NumPy array: ", num_array)

# Printing the element at position 0
print("The element at position 0 of the NumPy array: ", num_array[0])

# Printing the element at position (0, 0)
print("The element at position (0,0) of the NumPy array: ", num_array[0][0])

# Printing the element at position (0, 1)
print("The element at position (0,1) of the NumPy array: ", num_array[0][1])

# Printing the element at position (1)
print("The element at position (1) of the NumPy array: ", num_array[1])

# Printing the element at position (1, 0)
print("The element at position (1, 0) of the NumPy array: ", num_array[1][0])

# Printing the element at position (1, 1)
print("The element at position (1, 1) of the NumPy array: ", num_array[1][1])
num_array=[123456]\text{num\_array} = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}

You can take these concepts of indexing even further and generalize it for n-dimensional arrays.

NumPy Array Slicing

Slicing a NumPy array refers to accessing/retrieving elements in between a starting and ending index position of an array.

Parameters:

  • start: The starting index position from where the slice should happen. If start is not specified, it is set as 0 by default.
  • end: The ending index position till where the slice should happen. If end is not specified, it is set as the length of the array by default.
  • step: The distance between two adjacent values in the array to be sliced. The default step size is 1.

The following example showcases how to slice a 1D NumPy array in different ways:

python
# Creating a 1D NumPy array
num_array = np.array([1, 2, 3, 4, 5])

print("The content of the NumPy array: ", num_array)

print("Elements starting from index 1:", num_array[1:])
print("Elements from index 1 to index 5: ", num_array[1:5])
print("Elements starting from index 1 to index 5 with a step of 2: ", num_array[1:5:2])
print("Elements starting from index 1 with a step of 2: ", num_array[1::2])
print("Elements starting with a step of 2: ", num_array[::2])
print("Elements till and not including index 1: ", num_array[:1])
print("Elements till and not including index 4: ", num_array[:4])
print("Elements till and not including index 4 with a step of 2: ", num_array[:4:2])
print("Elements except the last two indexes: ", num_array[:-2])
print("Elements starting from the second last index: ", num_array[-2:])
print("All elements of the array: ", num_array[:])
num_array=[12345]\text{num\_array} = \begin{bmatrix} 1 & 2 & 3 & 4 & 5 \end{bmatrix}