Selecting a single element from a NumPy array

Each element of these ndarrays can be accessed using its index number.

Example: The following code shows how to access an element of a NumPy array.

Python3




import numpy as np
  
# NumPy Array
numpyArr = np.array([1, 2, 3, 4])
print("numpyArr[0] =", numpyArr[0])
print("numpyArr[-1] =", numpyArr[-1])


Output:

numpyArr[0] = 1
numpyArr[-1] = 4

In the first case, we accessed the first element of the array using its index number. In the second case we accessed the last element of the array by using negative indexes.

Select an element or sub array by index from a Numpy Array

The elements of a NumPy array are indexed just like normal arrays. The index of the first element will be 0 and the last element will be indexed n-1, where n is the total number of elements.

Similar Reads

Selecting a single element from a NumPy array

Each element of these ndarrays can be accessed using its index number....

Selecting a sub array from a NumPy array (Slicing)

...