0

so I have a list x = [1,2,3]

when I run print(x[0:100])

it still prints out 1,2,3 I would expect an index out of range error.

  • Slicing does not throw an error for indexes that are out of range. Sometimes this can be used for good. For example, if you want to check if a string begins with a particular letter, `x[0]` gives an error if `x` contains no characters, but `x[0:1]` doesn't. – kindall Jun 28 '22 at 23:55
  • @Lucas Styles answered already, but here's a simple reference in future about slicing in Python: https://stackoverflow.com/a/509295/14715054 – crock Jun 28 '22 at 23:55
  • It is the same for lists as for any other sequence type. – Karl Knechtel Jun 29 '22 at 00:02

1 Answers1

1

It is just returning every element in the list up to 100. It doesn't require that there are 100 elements in the list. If you did x[100] it would error