Array Indexing

Array indexing is accessing array elements. In Numpy, access an array element using the index number. The 0th index is element 1 and the flow goes on as shown below:

  •   index 0 – element 1
  •   index 1 – element 2
  •   index 2 – element 3
  •   index 3 – element 4

In this lesson, we will cover the following topics to understand Array Indexing in NumPy:

  • Access elements from a 1D Array
  • Access elements from a 2D Array
  • Access elements from a 3D Array
  • Access elements from the last with Negative Indexing

Let us begin with accessing elements from a One-Dimensional i.e. 1D array:

Access elements from a One-Dimensional array

The following are some examples to access specific elements from a 1D array:

Example: Access the 1st element (index 0) from a One-Dimensional array

import numpy as np

n = np.array([10, 20, 30, 40, 50])
print(n[0])

Output

10

Example: Access the 4th element (index 3) from a One-Dimensional array

import numpy as np

n = np.array([10, 20, 30, 40, 50])
print(n[3])

Output

40

Access elements from a Two-Dimensional array

Accessing elements work as a matrix in a 2D Array i.e.

a[0,0] = dimension 1 element 1st
a[0,1] = dimension 1 element 2nd 
a[0,2] = dimension 1 element 3rd
 
a[1,0] = dimension 2 element 1st 
a[1,1] = dimension 2 element 2nd 
a[1,2] = dimension 2 element 3rd

a[2,0] = dimension 3 element 1st  
a[2,1] = dimension 3 element 2nd
a[2,2] = dimension 3 element 3rd

Following are some examples to access specific elements from a 2D array:

Example: Accessing 1st dimension elements from a 2D array

import numpy as np

n = np.array([[1,3,5],[4,8,12]])

print(n[0,0])
print(n[0,1])
print(n[0,2])

Output

1
3
5

Example: Accessing 2nd dimension elements from a 2D array

import numpy as np

n = np.array([[1,3,5],[4,8,12]])
print(n[1,0])
print(n[1,1])
print(n[1,2])

Output

4
8
12

Access elements from a Three-Dimensional Array

Following are some examples to access specific elements from a 3D array:

Example1

import numpy as np

n = np.array([[[5,10,15],[20,25,30]],[[35,40,45],[50,55,60]]])
print(n[0,0,0])
print(n[0,0,1])
print(n[0,0,2])

Output

5
10
15

Example2

import numpy as np

n = np.array([[[5,10,15],[20,25,30]],[[35,40,45],[50,55,60]]])

print(n[1,0,0])
print(n[1,0,1])
print(n[1,0,2])

Output

35
40
45

Access the array from the last with Negative Indexing

Arrays can be accessed with negative indexing. This gives the last element.

Example 1: Access the last element from a 1D array with negative indexing

import numpy as np

n = np.array([5, 10, 15])
print('Last element = ', n[-1])

Output

Last element =  15

Example 2: Access the last element from a 2D array with negative indexing

import numpy as np

n = np.array([[1, 3, 5], [4, 8, 12]])
print('Last element = ', n[0, -1])

Output

Last element =  5

Let us see another example:

import numpy as np

n = np.array([[1,3,5],[4,8,12]])
print('Last element = ', n[1,-1])

Output

Last element =  12

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.

For Videos, Join Our YouTube Channel: Join Now


Read More:

NumPy Tutorial
NumPy - Array Slicing
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment