Why does python allow list[a:b] but not list[a] if a and b are out of index range?
It is basically a design choice of Python, and there is not really something right or wrong with either an error for x[100:101]
versus giving an empty list
.
Note that x[slice(...)]
will always return a container (with the same type of x
), while x[int]
will always access the element at the specified position.