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

T J tjhnson@gmail....
Sun Aug 9 01:38:01 CDT 2009


On Sat, Aug 8, 2009 at 10:09 PM, David Warde-Farley<dwf@cs.toronto.edu> wrote:
> On 9-Aug-09, at 12:36 AM, T J wrote:
>
>>>>> z = array([1,2,3,4])
>>>>> z[[1]]
>> array([1])
>>>>> z[(1,)]
>> 1
>>
> 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].
>

Sure, but that wasn't my question.

I was asking about the difference between indexing with a 1-tuple (or
scalar) and with a 1-list.  Naively, I guess I didn't expect there to
be a difference.  Though, I can see its uses (through the z[z<3]
example).

The dictionary example is nice in that is really highlights exactly
*how* different arrays are from python dictionaries (aside from the
obvious): since lists are unhashable, you can't index with them at
all.  Yet you can index numpy arrays with lists AND the behavior is
different from if you indexed with a tuple!

>>> z = array([1,2,3])
>>> i = [2]
>>> type(z[i])
<type 'numpy.ndarray'>
>>> type(z[tuple(i)])
<type 'numpy.int32'>


More information about the NumPy-Discussion mailing list