Get Index of the element in python list

In this tutorial we will be focusing on how to extract the index of the element in the python list. We will be trying out different ways to extract the index for a specified value or element of the list in python. First, we will search for the position of the specified value. We will also see an example on index search between two ranges. Also, we have provided examples for finding the index of the first occurrence the element in the list and last occurrence of the element in the list in brief we will be covering

  • Extract the index(position) for a specified value in python list
  • Get the index from the specified position
  • Index search between two specified position in python list
  • Finding the index of the first occurrence the element in the python list
  • Finding the index of the last occurrence the element in the python list

 

Syntax

The basic syntax for getting the index from python list is:

list_name.index(element, start, end)

Parameters:  

  • element– The element whose lowest index(first occurrence) will be returned.
  • start(Optional) – The position from where the search begins.
  • end(Optional) – The position from where the search ends.

 

get-index-of-an-element-in-python-list-1

 

Extract the index for a specified element in list python:

First let’s create the simple list as shown below

# Create a simple list
vowels = ['a', 'e', 'i', 'o', 'i', 'u', 'e', 'i']
vowels

 

Now lets find the index of vowel ‘e’.  This can be achieved by Listname.index()  and passing value to it  Listname.index(‘value’)  as shown below.

Example 1:

# Get the index of vowel e
vowels.index('e')

output:

1

 

 

Example 2:

Let’s get the index for vowel “i”. by default first occurrence of “i” will be resulted


# Get the index of vowel i

vowels.index('i')

output:

2

 

 

 

Get the index from the specified position:

Now let’s look for the index “i” from the 3rd position onwards. So it will get the position of value “i” only from 3rd position

#### index search from specific position

vowels.index('i',3)

output:

4

 

 

Index search between two specified position ranges in python list:

In the below example we will be looking at the index of value ”i” between 5th and 7th position (index)

#### index search between two specific position ranges

vowels.index('i',5,7)

output:

6

 

 

Finding the index of the first occurrence the element in the python list:

By default list_name.index() function gets the index of the first occurrence of the elements in the python

### Find the index of the first occurence in the list

vowels.index('i')

output:

2

 

 

Finding the index of the last occurrence the element in the python list:

Method 1 :

In order to find the last occurrence in the list we will be using join() + rindex() function.  ”.join(vowels) will convert all elements of list to string  i.e. [‘a’, ‘e’, ‘i’, ‘o’, ‘i’, ‘u’, ‘e’, ‘i’] will become ‘aeioiuei’ and the rindex(‘i’) method finds the last occurrence of the specified value ‘i’. which is 7

#########find the index of the last occurrence in the list: Method 1 using join() + rindex()

''.join(vowels).rindex('i')

output:

7

 

Method 2:

In this method we will be reversing the list and then finding the index and subtracting the results from len(vowels) – 1 so the result will be 7

###### find the index of the last occurence in the list: Method 2 using List Slice + index()

len(vowels) - 1 - vowels[::-1].index('i')

output:

7

 

Method 3:

In this method we will be using max() function to return the maximum index of the value ‘i’

# to get index of the last occurence of element python list

max(idx for idx, val in enumerate(vowels) if val == 'i')

output:

7

 

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts