Negative list index

Negative numbers mean that you count from the right instead of the left. So, list[-1] refers to the last element, list[-2] is the second-last, and so on. List indexes of -x mean the xth item from the end of the list, so n[-1] means the last item in the list n .

  • A negative index accesses elements from the end of the list counting backwards.

An example to show negative index in python

>>> import array
>>> a= [1, 2, 3]
>>> print a[-3]
1
>>> print a[-2]
2
>>> print a[-1]
3

你可能感兴趣的:(Negative list index)