Negative IndexingNegative Indexing

We talked about the usual indexing. But besides this there is also negative indexing. Negative indexing means starting from the end, i.e. index -1 refers to the last element, index -2 refers to the penultimate element, and so on.

The example shows how we can get a value of 10 from a given two-dimensional array using negative indexing. The first index is responsible for which row we choose (-1 is the last). Another index corresponds to this element, which we select in this row (-1 is the last one). As a result, we got a value of 10. Run the code above and you will see it.



You have such an array: [[[-4, 3, 1], [-4, 39, 8]], [[2, -4, 10], [15, 193, 8]]]

  1. You have to access 10.

Let's try. Use only negative indexes.

Everything was clear?

Section 3. Chapter 5
toggle bottom row