The above code will output
, and will not result in an
As one would expect, attempting to access a member of a list using an index that exceeds the number of members (e.g., attempting to access
list in the list above) results in an
IndexError. However, attempting to access a slice of a list at a starting index that exceeds the number of members in the list will not result in an
IndexError and will simply return an empty list.
What makes this a particularly nasty gotcha is that it can lead to bugs that are really hard to track down since no error is raised at runtime.
ENJOYING THIS POST?
Get 5% discount on your monthly subscription purchase
If you want to explore more, visit our Python edu & tutorials section! Below are some examples: