Simple slicing question
Peter Hansen
peter at engcorp.com
Mon Jan 14 23:37:58 EST 2002
"Nikhil R. Deshpande" wrote:
>
> Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> a = [1, 2, 3]
> >>> a[100:200]
> []
>
> Why doesn't a[100:200] result in an IndexError?
The other answers are all wonderful, but I want to point out
that the values 100 and 200 in a[100:200] are *not* indices,
and therefore an IndexError should definitely not be thrown.
In the expression a[100] you are selecting the hundred-and-first
element (starting from zero, of course), and that's an index.
In a[100:200] you are selecting everything *between* the
hundredth and two-hundredth elements, not both of those
elements.
You might argue for a SliceError, but not an IndexError.
More information about the Python-list
mailing list