Array Indexing in Python | Explained With Examples

In Python, array indexing means accessing the elements from the arrays using index numbers. The positive and negative indexing is used to access the element of arrays from start and end. The array indexing feature allows for more efficient manipulation and use of arrays. The index value starts from “0” for the first items and increments by “1” for each subsequent item.

This post provides a complete guide on arrays indexing using the below contents: 

How to Index an Array in Python?

To index an array, the standard accessing syntax, i.e., “array[obj]” is used in Python. In this syntax, “obj” specifies the index of an array element that needs to be accessed from the arrays. For example, an “array[0]” accesses the first element/items of the array, an “array[1]” accesses the second element/items, and so on.

Let’s understand the array indexing methods using numerous examples:

Example 1: Indexing 1D Arrays

The given below example is utilized to access the element of the 1-D array using an index value:

Code:

import numpy
array_value = numpy.array([45, 55, 52, 15, 25])
print(array_value[4])
  • The “numpy.array()” function is utilized to create the array. 
  • The “array_value[]” takes the integer as an argument and accesses the specific index value.

Output:

The element value placed at the specific index has been accessed.

Example 2: Indexing 2D Arrays

The below example code is used to access the elements of 2-D arrays using the specific index value:

Code:

import numpy
array_value=numpy.array([[45, 55, 23, 32],[24,35,36,45]])
print(array_value[0,2])
  • The expression “array_value[0,2]” means that the element is specified using the two integers inside square brackets after the array name. 
  • The first integer is 0 and it represents the row index. 
  • The second integer is 2 and it represents the column index. 

Output:

The value of the items in the “1st” row and the “3rd” column of the array object is printed successfully.

Example 3: Indexing 3D Arrays

The below code access the element of the 3-D array using the index value:

Code:

import numpy
array_value= numpy.array([[[21, 32, 13], [44, 25, 46]],
                      [[72, 84, 29], [20, 31, 15]]])
print(array_value[1, 1, 2])
  • The “numpy.array()” function of the numpy module is used to create the “3-dimensional” array.
  • The expression “array_value[1, 1, 2]” is used to print the value of a specific element in the array using indexing.
  • First, we accept the second element of axis 0 (the second two-dimensional array).
  • Secondly, the second element of axis 1 (the second row) is selected from the array.
  • Lastly, the third element of axis 2 (the third column) is selected from that specific row.

Output:

The specific element value has been accessed from the “3-dimensional” array.

Example 4: Indexing Multidimensional Arrays

To store data of different types and of different sizes in a single array, the multidimensional array is used in Python. The below code is used to access the elements of the multidimensional arrays:

Code:

import numpy
array_value = numpy.arange(9).reshape(3,3)
print('3x3 Array: \n',array_value)
print('\nElement at Row 1 and Column 2: ',array_value[1,2])
print('\nAccess a Subarray Using Slicing: \n',array_value[0:2,1:])
  • The module named “numpy” is imported.
  • The “numpy.arange()” function is used to create a 1D array of 9 elements from 0 to 8.
  • The “reshape()” function is used to reshape it into a 2D array of 3 rows and 3 columns.
  • The expression “array_value[1,2]” is used to display the element value at row index 1 and column index 2 of the array.
  • The expression “array_value[0:2, 1:]” shows the subarray of the original array using slicing notation. In other words the subarray [0:2,1:] access rows from index 0 (inclusive) to index 2 (exclusive) and columns from index 1 (inclusive) to the end.

Output:

The multidimensional array has been successfully indexed using slicing notation.

Example 5: Negative Indexing in Python

The negative indices are also used to access elements/items from the end of the array. Here is an example code:

Code:

import numpy
array_value = numpy.array([44, 45, 54, 32, 54])
print('Accessing Element Using Negative Index: ', array_value[-3])
  • The “numpy.array()” function is utilized to create the array.
  • The negative value is used to access the array element values from the end side of the array. The last element/item of the given array is considered as “-1”.

Output:

The specific element of the array has been selected from the end side using a negative index.

Example 6: Using Array Indexing to Perform Arithmetic Operation

The following example uses an arithmetic operator to perform mathematical calculations on the array element:

Code:

import numpy
value=numpy.array([44, 45, 54, 32, 54])
print('Addition of Two Elements of an Array: ', value[1]+value[3])
print('\nsubtraction of Two Elements of an Array: ',value[3]-value[1])
print('\nMultiplication of Two Elements of an Array: ',value[2]*value[4])
print('\nDivision of Two Elements of an Array: ',value[2]/value[4])
  • The array is created using the “numpy.array()” function.
  • The element value is accessed using the square bracket notation.
  • Different operations such as subtraction, addition, multiplication, and division are executed on the access element value of the array.

Output:

The arithmetic operations have been performed successfully on array elements.

Conclusion

In Python, “array indexing” is used to access the specific elements/items in an array or list using their index values. By using the index value, we can access the element value of 2-D, 3-D, and any multidimensional array. The negative indices can be used to access elements from the end side of the specified array. Multiples arithmetic operators such as +, -, *, and / can also perform calculations on the accessed elements value of the arrays. This guide presented a complete guide on array indexing using numerous examples.