[Numpy-discussion] Indexing with a list...

David Warde-Farley dwf@cs.toronto....
Sun Aug 9 00:09:25 CDT 2009

On 9-Aug-09, at 12:36 AM, T J wrote:

>>>> z = array([1,2,3,4])
>>>> z[[1]]
> array([1])
>>>> z[(1,)]
> 1
> I'm just curious: What is the motivation for this differing behavior?

When you address, i.e. an element in 2D array with a[2,3] you are  
actually indexing z with a tuple object (2,3). The 'comma' operator in  
Python creates a tuple, irrespective of whether you use parens or not.


In [192]: z = {}

In [193]: z[2,3] = 5

In [194]: z
Out[194]: {(2, 3): 5}

In the special case of scalar indices they're treated as if they are  
length-1 tuples. The behaviour you're seeing is the same as z[1].


More information about the NumPy-Discussion mailing list