Updated April 15, 2023
Introduction to NumPy Array Indexing
Whenever we want to access any element in the given array, we make use of the concept called indexing in arrays in python. Where indexing means referring to the index number of the element in the array using which the element of the array can be accessed. And the indexes for any given array in NumPy starts from zero, which means the index number of the first element in the given array is zero; likewise, the index number of the second element in the given array is one and so on, and this concept of accessing the elements of the given array using indexes is called array indexing in NumPy.
Syntax
The syntax for NumPy 3D array indexing in Python is as follows:
1. In the case of a one-dimensional array
numpy.arrayname(indexnumberofthearrayelement)
where arrayname is the name of the array from which elements of the array can be retrieved using indexes of the array and
indexnumberofthearrayelement represents the index number corresponding to each element in the given array represented by arrayname.
2. In the case of a two-dimensional array
numpy.arrayname(dimensionnumber, indexnumberofthearrayelement)
where arrayname is the name of the array from which elements of the array can be retrieved using indexes of the array,
dimensionnumber specifies the dimension of the array and
indexnumberofthearrayelement represents the index number corresponding to each element in the given array represented by arrayname.
3. In the case of a two-dimensional array
numpy.arrayname(dimensionnumber1, dimensionnumber2, indexnumberofthearrayelement)
where arrayname is the name of the array from which elements of the array can be retrieved using indexes of the array,
dimensionnumber1 specifies the first dimension of the array,
dimensionnumber2 specifies the second dimension of the array and
indexnumberofthearrayelement represents the index number corresponding to each element in the given array represented by arrayname.
Steps to perform array indexing in NumPy
- Whenever we want to access any element in the given array, we make use of the concept called indexing in arrays in python.
- Indexing means referring to the index number of the element in the array using which the element of the array can be accessed.
- The indexes for any given array in NumPy start from zero, which means the index number of the first element in the given array is zero; likewise, the index number of the second element in the given array is one and so on.
- The element in the given array can be accessed by specifying the name of the array followed by the index number of the element in the array in square brackets in case of a one-dimensional array.
- The element in the given array can be accessed by specifying the name of the array followed by the dimension number of the array and index number of the element in the array in square brackets in the case of the two-dimensional array.
- The element in the given array can be accessed by specifying the name of the array followed by the dimension number1 of the array, dimension number2 of the array, and index number of the element in the array in square brackets in case of a three-dimensional array.
Examples of NumPy Array Indexing
Different examples are mentioned below:
Example #1
Python program to demonstrate NumPy array indexing by creating a one-dimensional array and using indexing to print a specific element in the array as the output on the screen.
Code:
#importing the package numpy as pyn
import numpy as pyn
#creating an one dimensional array using the array function and storing it in the variable called onedimarray
onedimarray = pyn.array([1,2,3,4])
#displaying the elements of the newly created one dimensional array followed by an one line space
print 'The elements of the newly created one dimensional array are:'
print onedimarray
print '\n'
#Displaying the second element in the newly created one dimensional array using indexing in numpy
print 'The second element in the newly created one dimensional array is'
print onedimarray[1]
Output:
In the above program, the package numpy is imported as pyn to make use of numpy functions such as array and indexing. Then a one-dimensional array is created using the array function in numpy. Then the elements of the newly created one-dimensional array are displayed on the screen. Then by using the concept of indexing, we are retrieving the required element from the newly created array.
Example #2
Python program to demonstrate NumPy array indexing by creating a two-dimensional array and using indexing to print a specific element in the array as the output on the screen:
Code:
#importing the package numpy as pyn
import numpy as pyn
#creating a two dimensional array using the array function and storing it in the variable called twodimarray
twodimarray = pyn.array([[1,2],[3,4]])
#displaying the elements of the newly created two dimensional array followed by an one line space
print 'The elements of the newly created two dimensional array are:'
print twodimarray
print '\n'
#Displaying the first element in the newly created two dimensional array using indexing in numpy
print 'The first element in the newly created two dimensional array is'
print twodimarray[0,1]
Output:
In the above program, the package numpy is imported as pyn to make use of numpy functions such as array and indexing. Then a two-dimensional array is created using the array function in numpy. Then the elements of the newly created two-dimensional array are displayed on the screen. Then by using the concept of indexing, we are retrieving the required element from the newly created array.
Example #3
Python program to demonstrate NumPy array indexing by creating a three-dimensional array and using indexing to print a specific element in the array as the output on the screen:
Code:
#importing the package numpy as pyn
import numpy as pyn
#creating a two dimensional array using the array function and storing it in the variable called threedimarray
threedimarray = pyn.array([[[8,9], [3,4]], [[10,9], [8,6]]])
#displaying the elements of the newly created three dimensional array followed by an one line space
print 'The elements of the newly created three dimensional array are:'
print threedimarray
print '\n'
#Displaying the second element in the newly created three dimensional array using indexing in numpy
print 'The second element in the newly created three dimensional array is'
print threedimarray[0,1,1]
Output:
In the above program, the package numpy is imported as pyn to make use of numpy functions such as array and indexing. Then a three-dimensional array is created using the array function in numpy. Then the elements of the newly created three-dimensional array are displayed on the screen. Then by using the concept of indexing, we are retrieving the required element from the newly created array.
Recommended Articles
This is a guide to NumPy Array Indexing. Here we discuss the concept of NumPy array indexing in Python through definition, syntax, and retrieving the elements of the 1D, 2D, and 3D array in python through programming examples and their outputs. You may also have a look at the following articles to learn more –