why won't slicing lists raise IndexError?

Jason Maldonis jjmaldonis at gmail.com
Mon Dec 4 13:54:27 EST 2017


I was extending a `list` and am wondering why slicing lists will never
raise an IndexError, even if the `slice.stop` value if greater than the
list length.

Quick example:

my_list = [1, 2, 3]
my_list[:100]  # does not raise an IndexError, but instead returns the full
list

Is there any background on why that doesn't raise an IndexError? Knowing
that might help me design my extended list class better. For my specific
use case, it would simplify my code (and prevent `if isinstance(item,
slice)` checks) if the slicing raised an IndexError in the example I gave.



More information about the Python-list mailing list