Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Array Indexing in Python – Beginner’s Reference

Array Indexing In Python

Array Indexing means searching for elements in an array using the index (position) of elements for quick retrieval of information.

Getting Started with Array Indexing in Python

Python arrays are variables that consist of more than one element. In order to access specific elements from an array, we use the method of array indexing.

The first element starts with index 0 and followed by the second element which has index 1 and so on. NumPy is an array processing package which we will use further.

Let’s start this off with a few examples.

Indexing to retrieve the third element of the array

>>> import numpy as np
>>> a=np.array([10,20,30,40,50])
>>> print(a[2])
30

In the above example, there are five elements in the array. 10 has index 0, 20 has index 1, 30 has index 2, 40 has index 3 and 50 has index 4.

So to retrieve the third element of the array which is 30 we determined its index 2.

Indexing to retrieve the fifth element of the array

>>> import numpy as np
>>> a=np.array([10,20,30,40,50])
>>> print(a[4])
50

Arithmetic Operations using Array Indexing

Let’s perform arithmetic operations on individual elements of an array using indexing.

1. Adding two elements of an array using index

>>> import numpy as np
>>> a=np.array([10,20,30,40,50])
>>> print(a[1]+a[3])
60

2. Subtracting two elements of an array using index

>>> import numpy as np
>>> a=np.array([10,20,30,40,50])
>>> print(a[4]-a[0])
40

3. Multiply two elements of an array using index

>>> import numpy as np
>>> a=np.array([10,20,30,40,50])
>>> print(a[2]*a[3])
1200

4. Divide two elements of an array using index

>>> import numpy as np
>>> a=np.array([10,20,30,40,50])
>>> print(a[2]/a[3])
0.75

Indexing 2D Arrays in Python

2-Dimensional arrays in Python can be accessed using value, row, and columns. The general syntax for accessing specific elements from a 2D array is as follows:

Syntax : < value > = < array > [ row , column ] 

Here, <value> means the variable where the retrieved element from the array is stored. And [row, column] specifies the row and column index of the value.

Construct a 2D array and retrieve one element using array index.

>>> import numpy as np
>>> a=np.array([[1,2,3],[4,5,6]])
>>> print(a)
[[1 2 3]
 [4 5 6]]
>>> value=a[1,2]
>>> print(value)
6

Indexing 3D Arrays in Python

Following is the general syntax for accessing elements from a 3D array using index.

Syntax : array[first dimension, second dimension, third dimension]

Here the first, second and third numbers represent 1D, 2D and 3D respectively.

Construct a 3D array and retrieve one element using the array index.

>>> import numpy as np
>>> a= np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
>>> print(a[0, 1, 2])
6

Python Array Index (Multi-dimensional Arrays)

Indexing a multi-dimensional array is quite complex. Let us start with creating a simple multidimensional array. For creating a multidimensional array we will use reshape() and arange() methods.

  • The reshape() function takes a single argument that specifies the new shape of the array.
  • The arange() method is used in Numpy. It takes to start and end arguments and creates a single dimension array.
>>> import numpy as np
>>> arr=np.arange(10).reshape(5,2)
>>> print(arr)
[[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]
>>> import numpy as np
>>> arr=np.arange(12).reshape(2,2,3)
>>> print(arr[0:3])
[[[ 0  1  2]
  [ 3  4  5]]

 [[ 6  7  8]
  [ 9 10 11]]]

>>> print(arr[1:5:2,::3])
[[[6 7 8]

Conclusion

This was in brief about array indexing in the Python programming language. Hope this article proves helpful. You can learn more about array slicing in Python here.