0

Say I have the following list:

l = [0,1,2,3,4]

If I choose some arbitrary index, for example, 3, and then do a subset:

l[3:]

The result is [3, 4]

However, if I choose a start index > len(l) (eg l[6:]), I get the following result:

[]

Why do I get an empty list, rather than an exception? If I tried l[6], I would get:

IndexError: list index out of range

What is going on here? What is the reason for this behaviour?

pookie
  • 3,796
  • 6
  • 49
  • 105
  • [relevant](https://stackoverflow.com/questions/9490058/why-substring-slicing-index-out-of-range-works-in-python) – jacoblaw Aug 04 '17 at 21:16
  • 3
    Possible duplicate of [Why substring slicing index out of range works in Python?](https://stackoverflow.com/questions/9490058/why-substring-slicing-index-out-of-range-works-in-python) – Benjamin Lee Aug 04 '17 at 21:48
  • [This one](https://stackoverflow.com/questions/20033201/python-index-out-of-range-in-list-slicing) has a more concise answer than the original question it duplicated – Alter Aug 04 '17 at 21:58

0 Answers0