×

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.


Related Topics

How to Configure Python Interpreter in Eclipse

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

How to set up a proxy using selenium in python

What is Proxy? A proxy acts as a middleman between user requests and server responses. The main purposes of proxies are to protect user privacy and encapsulate data between various interactive...

3 minutes read.

Front end in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The...

4 minutes read.

Best Way to Learn Python for Free

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. Learning Python is a step towards coding. Python gets...

5 minutes read.

List Assignment Index out of Range in Python

As we know, a List is one of the four unique data structures available in Python; In this tutorial, we will deep dive into understanding iterating through a list and...

3 minutes read.

Crash Course on Python by Google

There is a new, free Python programming course offered by Google on Coursera. No programming experience is necessary. There are numerous Python courses available, but when you learn that Google is...

5 minutes read.

Python Slice from Last Occurrence of K

Introduction We already know that in Python, cutting produces a sub-string out of a string. The variables start, stop, and step are used to set the slicing range. When dealing on...

3 minutes read.

PyDev with Python IDE

What is IDE? Integrated Development Environment is the abbreviation of IDE. It is a coding tool that automates code editing, finding errors and testing. IDE combines all the developer tools into...

3 minutes read.

Filter List in Python

Python: Python is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a user-friendly programming...

3 minutes read.

Spell Corrector GUI using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Convert string into int in Python

String A string is defined as a series of characters, special characters, and numbers. A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements...

2 minutes read.

Convert String to Binary in Python

String to binary The strings can be defined as the array of Unicode code characters. Binary Binary is defined as the number system which consists two symbols 0 and 101. It is base-2...

2 minutes read.

An Introduction to Subprocess in Python with Examples

Subprocess in Python By starting new processes, the Python function subprocess is utilized to run extra programs and applications. It makes it possible to run brand-new apps straight from a Programming...

6 minutes read.

How to Import Files in Python

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Checking whether a String Contains a Set of Characters in python

In this tutorial, we will learn how to examine or check whether a string contains any set of characters or a substring and if it contains, we will learn how...

6 minutes read.

Continue and Pass Statements in Python

Difference between continue and pass statements in Python The tasks can be repeated and automated efficiently using loops in Python. Sometimes we have to exit from the entire loop completely, we...

2 minutes read.

Difference between Input() and raw_input() functions in Python

There are two input functions in Python that are used for taking input are given below: raw_input()input() What is raw_input() Function? raw_input() function is a built-in function in Python. It is used to...

6 minutes read.

Python String rfind() method

The string. rfind() method in Python returns the highest index in the string if the substring sub is found else it returns -1 if the substring is not found. Syntax string.rfind(sub [,start [,end]]) Parameter sub: This parameter...

2 minutes read.

Python String endswith() method

Python String endswith() method The string.endswith() method in Python returns a Boolean value True if the string ends with the specified suffix, otherwise it returns False. Syntax endswith(suffix[, start[, end]]) Parameter suffix – This parameter represents a string or tuple...

2 minutes read.

Confusion Matrix Visualization Python

The confusion matrix is a two-dimensional array that compares the anticipated and actual category labels. These are the True Positive, True Negative, False Positive, and False Negative classification categories for...

4 minutes read.