list negative indexing


The last element (rightmost) of the list has the index -1; its adjacent left element is present at the index -2 and so on until the left-most elements are encountered.

PROGRAM

list = [1,2,3,4,5]  
print(list[-1])  
print(list[-3:])  
print(list[:-1])  
print(list[-3:-1]) 


Output:

5
[3, 4, 5]
[1, 2, 3, 4]
[3, 4]