How to select NumPy array elements

NumPy array elements can be easily selected. In order to demonstrate that we need an array. Let’s create one using the array() function.

import numpy as np

a = np.array([[1, 2], [3,4]])
a

We create the array by giving the array() function a list of lists.

The output will be:

array([[1, 2],
       [3, 4]])

Choosing elements of an array is very simple. We just have to employ the indices of an element of the array like is shown below.

For the top-left element:

a[0, 0]

#output
1

For the top-right element:

a[0, 1]

#output
2

For the bottom-left element:

a[1, 0]

#output
3

For the bottom-right element:

a[1, 1]

#output
4

Leave a Reply