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

David Warde-Farley dwf@cs.toronto....
Sun Aug 9 02:53:43 CDT 2009


On 9-Aug-09, at 2:38 AM, T J wrote:

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

Ah. I didn't see the relevance of z[z<3], but now I do. z < 3 produces  
a boolean *array*, and you're right that arrays and lists are treated  
the same. Single element lists and single element tuples are treated  
differently because tuples and lists are, in general; if the behaviour  
of list indices changed when they were length 1, you'd have all kinds  
of corner cases to check for and handle in cases where you don't know  
a priori know the length of your index list.

Since you can also have a tuple containing lists/arrays, that will  
pull out the elements on each axis in a shape-preserving way. And you  
can mix and match lists/arrays and slices, i.e. A[:,[4,1,5],[6,9,7]].

David


More information about the NumPy-Discussion mailing list