How to navigate by negative indexing
In Python we can navigate item in list by negative index. Negative indexing in Python allows you to access elements from the end of a sequence, such as a list or tuple. In Python, indexing starts from 0 for the first element from the left, and -1 for the first element from the right.The example below will show you to How to navigate by negative indexing.
For example:
myCar=['Toyota', 'Mitsubishi','Volvo']
print(myCar[-1])
#output: 'Volvo'
index( -1) is start from the end of list while index(-2) is the second end ‘Mitsubishi’ and ‘Toyota’ is the first element in list index(-3).
** Just Reminder**: index(0) is also the first element ‘Toyota’ , so the meaning is that :
index(-3) = index(0) = ‘Toyota’
index(-2)=index(1)=’Mitsubishi’
index(-1)=index(2)=’Volvo’
if list has more element than this example, the algorithm is still the same.