Numpy offers several ways to index into arrays. As mentioned earlier, items in numpy array object follow zero-based index. The three types of indexing methods that are followed in numpy  − field access, basic slicing, and advanced indexing.

Here are the 3 topics we will look into in this article:
1)    Slicing
2)    Integer array indexing
3)    Boolean array indexing

Slicing

Slicing of numpy array is similar to slicing a Python list. Since arrays may be multidimensional, you must specify a slice for each dimension of the array:

For one-dimensional array specify single slice

# slice items between indexes
import numpy as np
a = np.arange(10)
print(a[2:6]) #[2  3  4  5] 

For two-dimensional array specify 2 slices separated by the comma.

import numpy as np
# Create the following rank 2 array with shape (3, 4)
# [[ 11  12  13  14]
#  [ 25  26  27  28]
#  [ 39 30 31 32]]
a = np.array([[11,12,13,14], [25,26,27,28], [39,30,31,32]])
# Use slicing to pull out the subarray consisting of the first 2 rows
# and columns 1 and 2; b is the following array of shape (2, 2):
# [[12 13]
#  [26 27]]
b = a[:2, 1:3]
print(a[0, 1])   # Prints "12" 

To view a particular element from array mention the index along each axis. For example, in the last line of the code, a[0,1] will retrieve the second element from 1st row.

Integer array indexing

When you index into numpy arrays using slicing, the resulting array view will always be a subarray of the original array. In contrast, integer array indexing allows you to construct arbitrary arrays using the data from another array. Here is an example:

import numpy as np
a = np.array([[41,42], [43, 44], [45, 46]])
# An example of integer array indexing.
# The returned array will have shape (3,) and
print(a[[0, 1, 2], [0, 1, 0]])  # Prints "[41 44 45]" 

When fewer indices are provided than the number of axes, the missing indices are considered complete slices:

>>> b[-1]    # the last row. Equivalent to b[-1,:]
array([40, 41, 42, 43]) 

The expression within brackets in b[i] is treated as an i followed by as many instances of as needed to represent the remaining axes. NumPy also allows you to write this using dots as b[i,…].

The dots (…) represent as many colons as needed to produce a complete indexing tuple. For example, if x is a rank 5 array (i.e., it has 5 axes), then

  1. x[1,2,…] is equivalent to x[1,2,:,:,:],
  2. x[…,3] to x[:,:,:,:,3] and
  3. x[4,…,5,:] to x[4,:,:,5,:].
>>> c = np.array( [[[  0,  1,  2],   # a 3D array (two stacked 2D arrays)
...                 [ 10, 12, 13]],
...                [[100,101,102],
...                 [110,112,113]]])
>>> c.shape
(2, 2, 3)
>>> c[1,...]                                   # same as c[1,:,:] or c[1]
array([[100, 101, 102],
[110, 112, 113]])
>>> c[...,2]                                   # same as c[:,:,2]
array([[  2,  13],
[102, 113]]) 

Iterating over multidimensional arrays is done with respect to the first axis:

>>> for row in b:
...     print(row)
...
[0 1 2 3]
[10 11 12 13]
[20 21 22 23]
[30 31 32 33]
[40 41 42 43] 

However, if one wants to perform an operation on each element in the array, one can use the flat attribute which is an iterator over all the elements of the array.

Boolean array indexing

Boolean array indexing lets you pick out arbitrary elements of an array. Frequently this type of indexing is used to select the elements of an array that satisfy some condition. Here is an example:

import numpy as np
a = np.array([[1,2], [3, 4], [5, 6]])
 
# this returns a numpy array of Booleans of the same
# shape as a, where each slot tells
# whether that element of a is > 2.
 
print(a > 2)      # Prints "[[False False]
                     #          [ True  True]
                     #          [ True  True]]"
 
# We use boolean array indexing to construct a rank 1 array
# consisting of the elements of a corresponding to the True values
# of a > 2
 
print(a[a > 2])     # Prints "[3 4 5 6]" 

Here are the links to tutorials to get started with basics of Python.

Python Lists and its in-built functions

Top 20 Programming Terms that Everyone should know

Conditional Statements in Python (if, elif, else)

Learn about Classes and Objects in Python

Leave a comment on what you liked and what you didn’t liked about this tutorial.

Akarsh Singh
Follow me

Akarsh Singh

Founder, Code in Python. Web and Android App Developer. He loves Python and wants to share his knowledge via this website. Follow him on Facebook, Twitter and Github.
Akarsh Singh
Follow me

Latest posts by Akarsh Singh (see all)