[Tutor] a code question, but don't know question's name

Alan Gauld alan.gauld at btinternet.com
Sun Oct 7 17:29:17 CEST 2007

"Kent Johnson" <kent37 at tds.net> wrote

>> The notation data[;,0] doesn't make sense and is an error in 
>> Python.
> [:,0] is an extended slice, not an error:
> http://docs.python.org/ref/slicings.html

Really? I got an error from the interpreter.

>>> d[:,0]
Traceback (most recent call last):
  File "<input>", line 1, in ?
TypeError: list indices must be integers

I thought extended slices were double colon?...
but I see that's actually a "long slice"

Following the link I get the somewhat obscure explanation:

The semantics for an extended slicing are as follows.
The primary must evaluate to a mapping object,
This bit I understand :-)

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;

This seems to be the case here, but where is the tuple?

Is it the whole list and the zeroth item?

If i try testing anything I just get an error.
When were these introduced? I am on v2.4.3

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

And most of that I don't understand.
Can someone who does give some examples of this
and what you would use it for?

> It is used in Numeric/numpy to select from multidimensional arrays.

I don't use either, but since its part of the languyage I assiume it
can be used in normal Python lists too?


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list