**Greetings!**Some links on this site are affiliate links. That means that, if you choose to make a purchase, The Click Reader may earn a small commission

*at no extra cost to you*. We greatly appreciate your support!

Learn how to perform NumPy array indexing and slicing in Python.

## 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

The syntax for accessing the elements of a 1D NumPy array is as follows:

`numpy.array([elements])[i]`

Here, `numpy.array([elements])`

is a 1D NumPy array and `[i]`

refers to the index position of the `ith`

element.

The following example showcases how to access elements of a 1D NumPy array:

# 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])

The content of the NumPy array: [1 2 3] The first element of the NumPy array: 1 The second element of the NumPy array: 2 The third element of the NumPy array: 3

You can also use negative indexing to access elements of a NumPy array. This means that the last element of a NumPy array is indexed as -1, the second last element of a NumPy array is indexed as -2, and so on.

So, the same 1D NumPy array with negative indexing can be understood as the following.

# 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 -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])

The content of the NumPy array: [1 2 3] The first element of the NumPy array: 1 The second element of the NumPy array: 2 The third element of the NumPy array: 3

### Accessing elements of a 2D NumPy Array

The syntax for accessing the elements of a 2D NumPy array is as follows:

`numpy.array([[elements]])[i][j]`

Here, `numpy.array([[elements]])`

is a 2D NumPy array and `[i]`

refers to the index position of the `ith`

element in the first axis and `[j]`

refers to the index position of the `jth`

element in the second axis.

The following example showcases how to access elements of a 2D NumPy array:

# 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], [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])

The content of the NumPy array: [[1 2 3] [4 5 6]] The element at position 0 of the NumPy array: [1 2 3] The element at position (0,0) of the NumPy array: 1 The element at position (0,1) of the NumPy array: 2 The element at position (1) of the NumPy array: [4 5 6] The element at position (1, 0) of the NumPy array: 4 The element at position (1, 1) of the NumPy array: 5

You can take this concept 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. The general syntax for NumPy array slicing is as follows:

`numpy.array()[start:end:step]`

Here, `numpy.array()`

is a NumPy array and,

`start`

refers to the starting index position from where the slice should happen. If`start`

is not specified, it is set as 0 by default.`end`

refers to 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`

refers to 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:

# Importing the NumPy library as np import numpy as np # 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[:])

The content of the NumPy array: [1 2 3 4 5] Elements starting from index 1: [2 3 4 5] Elements from index 1 to index 5: [2 3 4 5] Elements starting from index 1 to index 5 with a step of 2: [2 4] Elements starting from index 1 with a step of 2: [2 4] Elements starting with a step of 2: [1 3 5] Elements till and not including index 1: [1] Elements till and not including index 4: [1 2 3 4] Elements till and not including index 4 with a step of 2: [1 3] Elements except the last two indexes: [1 2 3] Elements starting from the second last index: [4 5] All elements of the array: [1 2 3 4 5]

That is it for this lesson on indexing and slicing NumPy array.