We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

Slicings

A slicing selects a range of items in a sequence object (e.g., a string, tuple or list). Slicings may be used as expressions or as targets in assignment or del statements. The syntax for a slicing:

Syntax:

primary []

slicing ::= [simple_slicing][1] | [ extended_slicing][2] simple_slicing ::= [ primary][3] “[” [short_slice][4] “]” extended_slicing ::= [ primary][3] “[” [slice_list][5] “]” slice_list ::= [ slice_item][6] (“,” [slice_item][6])* [“,”] slice_item ::= [ expression][7] | [proper_slice][8] | [ ellipsis][9] proper_slice ::= [ short_slice][4] | [long_slice][10] short_slice ::= [[lower_bound][11]] “:” [[upper_bound][12]] long_slice ::= [ short_slice][4] “:” [[stride][13]] lower_bound ::= [ expression][7] upper_bound ::= [ expression][7] stride ::= [ expression][7] ellipsis ::= “…”

There is ambiguity in the formal syntax here: anything that looks like an expression list also looks like a slice list, so any subscription can be interpreted as a slicing. Rather than further complicating the syntax, this is disambiguated by defining that in this case the interpretation as a subscription takes priority over the interpretation as a slicing (this is the case if the slice list contains no proper slice nor ellipses). Similarly, when the slice list has exactly one short slice and no trailing comma, the interpretation as a simple slicing takes priority over that as an extended slicing.

Simple Slicing

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).

Extended Slicing

The semantics for an extended slicing are as follows. The primary must evaluate to a mapping object, and it is indexed with a key that is constructed from the slice list, as follows: if the slice list contains at least one comma, the key is a tuple containing the conversion of the slice items; otherwise, the conversion of the lone slice item is the key.

The conversion of a slice item that is an expression is that expression. The conversion of an ellipsis slice item is the built-in Ellipsis object. The conversion of a proper slice is a slice object (see types) whose start, stop and step attributes are the values of the expressions given as lower bound, upper bound and stride, respectively, substituting None for missing expressions.

Slicings as targets

Slicings can also appear as assignment targets.

The primary expression in the reference is evaluated. It should yield a mutable sequence object (such as a list). The assigned object should be a sequence object of the same type. Next, the lower and upper bound expressions are evaluated, insofar they are present; defaults are zero and the sequence’s length. The bounds should evaluate to (small) integers. If either bound is negative, the sequence’s length is added to it. The resulting bounds are clipped to lie between zero and the sequence’s length, inclusive. Finally, the sequence object is asked to replace the slice with the items of the assigned sequence. The length of the slice may be different from the length of the assigned sequence, thus changing the length of the target sequence, if the object allows it.