June 2017
Beginner
352 pages
8h 39m
English
One very convenient feature of lists (and other Python sequences, for this applies to tuples too) is the ability to index from the end, rather than from the beginning. This is achieved by supplying negative indices. For example:
>>> r = [1, -4, 10, -16, 15]>>> r[-1]15>>> r[-2]-16
Negative integers are −1 based backwards from the end, so index −5 is the last but fourth element as shown in the following diagram:

This is much more elegant than the clunky equivalent of computing a positive index, which you would otherwise need to use for retrieving that last element:
Read now
Unlock full access