Get index of element in array in python
Index :
Index is a number where the element is located in the given list. In other words it is the position of the element in the list. Index can also be treated as address so that it can access the data across the data frame.
Array :
An array is a collection of homogeneous elements in a single variable name under a single data type. In python arrays is the data type that can be created using numpy package.
In numpy arrays are mainly used for numerical analysis. Arrays can only consists of single data type. In order to access an array first we need to import the package and then we can use array() function to create an array.
Arrays are used to store large amount of data in a single variable.arrays can manage both numerical and arithmetic operations. In arrays we may use many functions such as append () , clear () , copy () , count () , extend () , index () , insert () , pop () , remove () , reverse () , sort () . Let us now discuss about index () function in python.
Index () :
In python, index is used as function. It is declared as index() . The index() function the array is used to find the index position of an element .
It is also used to find the index position of an item in string . It is also used to find the index of array of items .
index() is an inbuilt-function used in python. The arrays in the python are 0-indexed . The indexing in done in such a way that first element has index 0, second element as index 1, third element has index 2 and so on.
If there are n number of elements in an array then the index of nth element will be n-1. index() function searches for the given element from the starting to the ending of an array.
->In case of multiple elements it retrieves the element that as lower index than compared to other elements.
->If the element is not present in the array then it gives the output as a value error.
->It always retrieves the first matching element from the array.
Array Indexing :
Array indexing in python is used to access the elements in an array using the index of an element in the array. We can access the elements in the array by the help of the index at which the element is placed.
In arrays the indexing starts from ‘ 0 ‘ and it ends at ‘ n-1 ‘ where n is the number of elements in an array.
Example :
Accessing 1D array :
Creating the one dimensional array by importing the numpy package and then accessing the one dimensional array element using the index of the element.
Syntax to access 1D array element using index :
print ( < array > [ index ] )
Code 1 :
import numpy as np
arr1 = np . array ( [ 2 , 5 , 9 , 3 ] )
value1 = arr1 [ 0 ]
print ( value1 )
Output :
2
Code 2 :
import numpy as np
arr2 = np . array ( [ 7 , 9 , 5 , 8 , 3 , 1 ] )
value2 = arr2 [ 4 ]
print ( value2 )
Output :
3
Here in this example we have created an one dimensional array by importing numpy and then we have accessed the element using the index of an element.
Accessing 2D array :
Creating the two dimensional array by importing the numpy package and then accessing the two dimensional array element using the index of the element as rows and columns.
Syntax to access 2D array element using index as row and column :
print ( < array > [ row , column ] )
Code 1 :
import numpy as np
arr1 = np . array ( [ 8 , 9 , 5 , 2 , 7 ] , [ 7 , 6 , 5 , 9 , 1 ] )
value1 = arr1 [ 0 , 3 ]
print (‘ The 3rd element in 1st row is : ‘ value1 )
Output :
The 3rd element in 1st row is : 2
Code 2 :
import numpy as np
arr2 = np . array ( [ 4 , 8 , 9 , 15 , 19 , 17 ] , [ 9 , 7 , 3 , 8 , 2 , 19 ] )
value2 = arr2 [ 1 , 5 ]
print ( ‘ The 6th element in 2nd row is : ‘ value2 )
Output :
The 6th element in 2nd row is : 19
Here in this example we have created a two dimensional array by importing numpy and then we have accessed the element using the index of an element as rows and columns.
Accessing 3D array :
Creating the three dimensional array by importing the numpy package and then accessing the three dimensional array element using the dimensions and index of the element.
Syntax to access 1D array element using index :
print ( < array > [row , column , index] )
Code 1 :
import numpy as np
arr1 = np . array ( [ [ [ 7 , 5 , 6 ] , [ 3 , 4 , 9 ] ] , [ [ 2 , 9 , 6 ] , [ 9 , 8 , 7] ] ] ] )
value1 = arr1 [ 0 , 1 , 2 ]
print ( value1 )
Output :
9
Code 2 :
import numpy as np
arr2 = np . array ( [ [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] , [ [ 7 , 8 , 9 ] , [ 10 , 11 , 12] ] ] )
value2 = arr1 [ 0 , 0 , 0 ]
print ( value2 )
Output :
1
Here in this example we have created a three dimensional array by importing numpy and then we have accessed the element using the dimensions and index of an element.
In such a way we can access elements using the index.