×

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

Python Dictionary pop() method

Python Dictionary pop() method The dictionary.pop() method in removes the specified item and returns an element from a dictionary having the given key. Syntax dictionary.pop(key[, default]) Parameter key – This argument signifies the key which is to...

2 minutes read.

Iterators in Python

Introduction In Python, an iterator is defined as an object that enables traversing through all the values of a collection. It contains the countable number of values. The iterator is utilized to...

4 minutes read.

Python | Read csv using pandas.read_csv()

Python is an excellent language for performing information analysis, owing to the fantastic biological system of information-driven python packages. Pandas is one of those packages that make taking in and...

4 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.

Struct Module in Python

What is a structure in Python? An entity that holds data in form of a structure is called a data structure. In Python, the data structure includes tuples, lists, dictionaries, and...

4 minutes read.

Python Seaborn

Seaborn is an open-source library in Python that is used for data visualization and plotting graphs. The plots are used for the Visualization of data. It is built on top...

10 minutes read.

How to get Time in seconds in Python

Python's time module source shows that are based on time. The time() method is used by developers and returns the current time as a UNIX timestamp. A UNIX timestamp is...

4 minutes read.

Sklearn linear Model in Python

The most effective and reliable Python machine learning library is Sklearn. Through a Python consistency interface, it offers a variety of effective tools for statistical modeling and machine learning, including...

5 minutes read.

Python Variables, Constants and Literals

Python is today's most valuable, easy-to-implement and sought-out programming language. Nowadays, developers want to focus on implementing the program rather than spending time with complex programs. So, for this reason,...

17 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

CatPlot in Python

Python Seaborn Library Seaborn is a superb Python tool for displaying graphical statistics graphing. Seaborn provides different color schemes and attractive default styles to facilitate the creation of various statistics charts...

8 minutes read.

Palindrome In Python

What is Palindrome? A Palindrome can be defined as the number or a string that resides unchanged when it is reversed. Example: 14341 Output: Yes, this is a Palindrome number Example: RACECAR Output: Yes, this...

2 minutes read.

Python getattr() function

Python getattr() function The getattr() function in Python returns the value of the named attribute for the given object. Syntax getattr(object, name[, default]) Parameter object: It is a required parameter which represents an object. name: This parameter represents the...

1 minute read.

Recursive Multiplication Python

Recursive Multiplication Python In this tutorial, we will learn how to write a Python program to get the multiplication of two numbers using the recursive approach. In the recursive multiplication approach, we...

2 minutes read.

How to Install Tweepy in Python

In this article, we will learn or understand how to install Tweepy in Python and what Tweepy is. Firstly, let’s understand What Tweepy is. As we all know, one of the...

3 minutes read.

Python vs PHP

Python Python is a high-level, object-oriented, interpreted language that is used to create independent program and algorithms for a variety of applications. It has a large library support base. In 1990,...

3 minutes read.

Python Program to Find the Greatest Among Three Numbers

Python Program to find the greatest among three numbers We have many approaches to get the largest of three numbers, and here we will discuss a few of them. This python...

2 minutes read.

GUI Calendar 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.

StandardScaler in Sklearn

When and How Should You Use StandardScaler? StandardScaler comes into play whenever the properties of the provided dataset change significantly within their ranges or are recorded in different measurement units. Since using...

4 minutes read.

Check if a String is Empty in Python

The string is one of the data types of Python. This data type stores all kinds of data but all the characters must be enclosed using double quotes (""). In...

5 minutes read.