0

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?

Rima
  • 1,447
  • 1
  • 6
  • 12
  • The relevant part of [the docs](https://docs.python.org/3/tutorial/introduction.html) is "However, out of range slice indexes are handled gracefully when used for slicing:" – Sayse May 24 '21 at 22:26
  • Thank you. It helped me to understand. – Rima May 24 '21 at 22:37

0 Answers0