0

I just observed a behavior with python lists and wanted why and how it works.

If I have a list say my_list = [5]
Why does my_list[1:] give me [] as the output but my_list[1] give me an IndexError

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
adsbawston
  • 55
  • 1
  • 1
  • 5
  • my_list[1] is to access 1st element and my_list[1:] is to position 1 to the end. Out of range slice indexes are handled gracefully in the list slicing. Please read slicing examples in https://docs.python.org/3/tutorial/introduction.html – Dev Anand Feb 03 '18 at 18:02
  • `my_list[1]` is actually for the second element, not first. Lists are 0-based. – Renato Byrro Feb 03 '18 at 18:04
  • yes, I consider my_list[0] as to access zeroth element. :) – Dev Anand Feb 03 '18 at 18:13

0 Answers0