Simple slicing question

Steve Holden sholden at holdenweb.com
Mon Jan 14 08:11:45 EST 2002


"Nikhil R. Deshpande" <dnikhil at ip.eth.net> 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?
>
Because it isn't supposed to!

I don't think anything has changed since 2.0 (and before), whose Python
Reference Manual says in section 5.3.3:

"""
The semantics for a simple slicing are as follows. The primary must evaluate
to a sequence object. The lower and upper bound expressions, if present,
must evaluate to plain integers; defaults are zero and the sys.maxint,
respectively. If either bound is negative, the sequence's length is added to
it. The slicing now selects all items with index k such that i <= k < j
where i and j are the specified lower and upper bounds. This may be an empty
sequence. It is not an error if i or j lie outside the range of valid
indexes (such items don't exist so they aren't selected).
"""

regards
 Steve
--
http://www.holdenweb.com/







More information about the Python-list mailing list