Simple slicing question

Steven Majewski sdm7g at Virginia.EDU
Mon Jan 14 17:31:06 EST 2002


On Mon, 14 Jan 2002, Alex Martelli wrote:

> Slicing is a very tolerant operation -- it will NEVER raise an
> IndexError.  Slice-boundaries outside the sequence-boundaries
> just result in an empty-slice, that's all.  And, why shouldn't
> they?  The situations is very different for *indexing*, which
> must return exactly one item, thus having no natural way to
> indicate index-out-of-bounds... save for the exception.

ditto on everything Alex said, and I would add:

	>>> a = [1,2,3]
	>>> a[:100]
	[1, 2, 3]

By design, that is the desired behaviour above. You want the first
100 items in a, or the whole array if len < 100.


>>> a[100:]
[]

Also be design. Which leaves this as the only consistent
interpretation of a[100:200]

>>> a[100:200]
[]


-- Steve Majewski






More information about the Python-list mailing list