Negative indexing in list [duplicate]

  list, python

List has 10 element. when printed out list element using negative index -20 it works fine even though there are only 10 element.

list1 = [1,2,3,4,5,6,7,8,9,10]
print(list1[-20:20])

output

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Can someone explain how negative indexing works?

Source: Python Questions

LEAVE A COMMENT