Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Negative Indexing | Indexing and Slicing
NumPy in a Nutshell
course content

Course Content

NumPy in a Nutshell

NumPy in a Nutshell

1. Getting Started with NumPy
2. Dimensions in Arrays
3. Indexing and Slicing
4. Important Functions

bookNegative Indexing

We discussed positive indexing, but there is also negative indexing. Negative indexing starts from the end, with index -1 referring to the last element, index -2 referring to the second-to-last element, and so on.

12345
import numpy as np arr = np.array([[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]) print(arr[-1 , -1])
copy

This example illustrates how to retrieve a value of 10 from a given two-dimensional array using negative indexing.

The first index determines the row we choose (-1 refers to the last row), while the second index corresponds to the element we select within that row (-1 is the last one). As a result, we obtain a value of 10. Run the code above to verify it.

Task

You have the following array: [[-4, 3, 1], [2, 10, -4]]. Access the value 10.

Let's give it a try. Use only negative indices.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 5
toggle bottom row

bookNegative Indexing

We discussed positive indexing, but there is also negative indexing. Negative indexing starts from the end, with index -1 referring to the last element, index -2 referring to the second-to-last element, and so on.

12345
import numpy as np arr = np.array([[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]) print(arr[-1 , -1])
copy

This example illustrates how to retrieve a value of 10 from a given two-dimensional array using negative indexing.

The first index determines the row we choose (-1 refers to the last row), while the second index corresponds to the element we select within that row (-1 is the last one). As a result, we obtain a value of 10. Run the code above to verify it.

Task

You have the following array: [[-4, 3, 1], [2, 10, -4]]. Access the value 10.

Let's give it a try. Use only negative indices.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 5
toggle bottom row

bookNegative Indexing

We discussed positive indexing, but there is also negative indexing. Negative indexing starts from the end, with index -1 referring to the last element, index -2 referring to the second-to-last element, and so on.

12345
import numpy as np arr = np.array([[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]) print(arr[-1 , -1])
copy

This example illustrates how to retrieve a value of 10 from a given two-dimensional array using negative indexing.

The first index determines the row we choose (-1 refers to the last row), while the second index corresponds to the element we select within that row (-1 is the last one). As a result, we obtain a value of 10. Run the code above to verify it.

Task

You have the following array: [[-4, 3, 1], [2, 10, -4]]. Access the value 10.

Let's give it a try. Use only negative indices.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

We discussed positive indexing, but there is also negative indexing. Negative indexing starts from the end, with index -1 referring to the last element, index -2 referring to the second-to-last element, and so on.

12345
import numpy as np arr = np.array([[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]) print(arr[-1 , -1])
copy

This example illustrates how to retrieve a value of 10 from a given two-dimensional array using negative indexing.

The first index determines the row we choose (-1 refers to the last row), while the second index corresponds to the element we select within that row (-1 is the last one). As a result, we obtain a value of 10. Run the code above to verify it.

Task

You have the following array: [[-4, 3, 1], [2, 10, -4]]. Access the value 10.

Let's give it a try. Use only negative indices.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 5
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt