User guide
NumPy User Guide, Release 1.9.0
2.4.2 Single element indexing
Single element indexing for a 1-D array is what one expects. It work exactly like that for other standard Python
sequences. It is 0-based, and accepts negative indices for indexing from the end of the array.
>>> x = np.arange(10)
>>> x[2]
2
>>> x[-2]
8
Unlike lists and tuples, numpy arrays support multidimensional indexing for multidimensional arrays. That means that
it is not necessary to separate each dimension’s index into its own set of square brackets.
>>> x.shape = (2,5) # now x is 2-dimensional
>>> x[1,3]
8
>>> x[1,-1]
9
Note that if one indexes a multidimensional array with fewer indices than dimensions, one gets a subdimensional array.
For example:
>>> x[0]
array([0, 1, 2, 3, 4])
That is, each index specified selects the array corresponding to the rest of the dimensions selected. In the above
example, choosing 0 means that remaining dimension of lenth 5 is being left unspecified, and that what is returned is
an array of that dimensionality and size. It must be noted that the returned array is not a copy of the original, but points
to the same values in memory as does the original array. In this case, the 1-D array at the first position (0) is returned.
So using a single index on the returned array, results in a single element being returned. That is:
>>> x[0][2]
2
So note that x[0,2] = x[0][2] though the second case is more inefficient a new temporary array is created after
the first index that is subsequently indexed by 2.
Note to those used to IDL or Fortran memory order as it relates to indexing. Numpy uses C-order indexing. That
means that the last index usually represents the most rapidly changing memory location, unlike Fortran or IDL, where
the first index represents the most rapidly changing location in memory. This difference represents a great potential
for confusion.
2.4.3 Other indexing options
It is possible to slice and stride arrays to extract arrays of the same number of dimensions, but of different sizes than
the original. The slicing and striding works exactly the same way it does for lists and tuples except that they can be
applied to multiple dimensions as well. A few examples illustrates best:
>>> x = np.arange(10)
>>> x[2:5]
array([2, 3, 4])
>>> x[:-7]
array([0, 1, 2])
>>> x[1:7:2]
array([1, 3, 5])
>>> y = np.arange(35).reshape(5,7)
>>> y[1:5:2,::3]
2.4. Indexing 21