Why doesn't Python throw an error for slicing out of bounds?
Question:
MATLAB throws an error for this:
>> a = [2,3,4]
>> a(3:4)
index out of bounds
If something similar is tried with Python, why isn’t it illegal?
>>> a = [2,3,4]
>>> a[2:3]
[4]
Isn’t the Index ‘3’ in python out of bounds, considering Numbering starts from Zero in Python?
Answers:
Slicing never raise error in python for out of bound indexes..
>>> s =[1,2,3]
>>> s[-1000:1000]
[1, 2, 3]
From the docs on string(applies to lists, tuples as well):
Degenerate slice indices are handled gracefully: an index that is too
large is replaced by the string size, an upper bound smaller than the
lower bound returns an empty string.
Docs(lists):
The slice of s
from i
to j
is defined as the sequence of items with
index k
such that i <= k < j
. If i
or j
is greater than len(s)
, use
len(s)
. If i
is omitted or None
, use 0
. If j
is omitted or None
, use
len(s)
. If i
is greater than or equal to j
, the slice is empty.
Out-of-range negative slice indices are truncated, but don’t try this for single-element (non-slice) indices:
>>> word = 'HelpA'
>>> word[-100:]
'HelpA'
You have a range there. As soon as one index from the range goes outside the bounds the process of extracting elements stops.
There are no errors in slicing in Python.
Because [2:3]
is from 4 to the next ele – 1, which returns 4.
Slicing never raises an error. The least it can do is return an empty list/tuple/string (depending on the type of course):
>>> a[12312312:]
[]
[start:end:step]
So index 2 is 4
, then end - 1
is index 2 which is 4
.
As others answered, Python generally doesn’t raise an exception for out-of-range slices. However, and this is important, your slice is not out-of-range. Slicing is specified as a closed-open interval, where the beginning of the interval is inclusive, and the end point is exclusive.
In other words, [2:3]
is a perfectly valid slice of a three-element list, that specifies a one-element interval, beginning with index 2 and ending just before index 3. If one-after-the-last endpoint such as 3 in your example were illegal, it would be impossible to include the last element of the list in the slice.
MATLAB throws an error for this:
>> a = [2,3,4]
>> a(3:4)
index out of bounds
If something similar is tried with Python, why isn’t it illegal?
>>> a = [2,3,4]
>>> a[2:3]
[4]
Isn’t the Index ‘3’ in python out of bounds, considering Numbering starts from Zero in Python?
Slicing never raise error in python for out of bound indexes..
>>> s =[1,2,3]
>>> s[-1000:1000]
[1, 2, 3]
From the docs on string(applies to lists, tuples as well):
Degenerate slice indices are handled gracefully: an index that is too
large is replaced by the string size, an upper bound smaller than the
lower bound returns an empty string.
Docs(lists):
The slice of
s
fromi
toj
is defined as the sequence of items with
indexk
such thati <= k < j
. Ifi
orj
is greater thanlen(s)
, use
len(s)
. Ifi
is omitted orNone
, use0
. Ifj
is omitted orNone
, use
len(s)
. Ifi
is greater than or equal toj
, the slice is empty.
Out-of-range negative slice indices are truncated, but don’t try this for single-element (non-slice) indices:
>>> word = 'HelpA'
>>> word[-100:]
'HelpA'
You have a range there. As soon as one index from the range goes outside the bounds the process of extracting elements stops.
There are no errors in slicing in Python.
Because [2:3]
is from 4 to the next ele – 1, which returns 4.
Slicing never raises an error. The least it can do is return an empty list/tuple/string (depending on the type of course):
>>> a[12312312:]
[]
[start:end:step]
So index 2 is 4
, then end - 1
is index 2 which is 4
.
As others answered, Python generally doesn’t raise an exception for out-of-range slices. However, and this is important, your slice is not out-of-range. Slicing is specified as a closed-open interval, where the beginning of the interval is inclusive, and the end point is exclusive.
In other words, [2:3]
is a perfectly valid slice of a three-element list, that specifies a one-element interval, beginning with index 2 and ending just before index 3. If one-after-the-last endpoint such as 3 in your example were illegal, it would be impossible to include the last element of the list in the slice.