How to get the index of an element in Python List

In this Python tutorial, we will discuss how to get the index of an element in a Python List. To understand various approaches, we’ll use some built-in functions to get the index of an element in a Python List.

As a Developer, while making the Python Project, I got the requirement to get the index of an element in a Python List. So, I came across multiple methods that we can use to get the index of an element in a Python List.. And in this tutorial, we will try to cover all of them.

Here we will see:

  • How to get the index of an element in Python List using list comprehension.
  • How to get the index of an element in Python List using index()
  • How to get the index of an element in Python List using enumerate()
  • How to get the index of an element in Python List using filter()
  • How to get the index of an element in Python List using for loop
  • How to get the index of an element in Python List using a while loop

How to get the index of an element in Python List using list comprehension

  • In this section, we will discuss how to get the index of an element in Python List using list comprehension.
  • You can use Python List Comprehension to get a list of all the indices for a specific element’s occurrences in a List.
  • We can obtain the index values, or positions, of all instances of a given item in the List using List comprehension.

Syntax:

Let’s have a look at the Syntax and understand the working of the list comprehension method in Python

[exp for items in iterator if condition]

Example:

Let’s take an example and check how to get the index of an element in Python List using list comprehension.

Source Code:

new_list = [62,84,29,82,29,29,11,29] 
 
print ("Input list : " ,new_list) 
 
new_result = [i for i in range(len(new_list)) if new_list[i] == 29] 
 
print ("Indexes at which item 29 is present: ",new_result) 

In the following given code first, we created a list ‘new_list’ and then used the list comprehension method to iterate the values of the list and set the condition if new_list[i] == 29] then it will return the index values of that specific numbers.

Here is the implementation of the following given code

How to get the index of an element in Python List using list comprehension
How to get index of element in Python List using list comprehension

This is how to get the index of element in Python List using list comprehension.

Read: Python list methods

How to get the index of an element in Python List using index()

  • Now let us understand how to get the index of an element in Python List using index().
  • An index is a built-in function in Python (). The element is passed as an argument and the index is returned by this function. We may determine the index of an entry in a list in Python by using this function.
  • This function takes two main parameters and the range of places within which the search is to be conducted is represented by the optional start and finish parameters.

Syntax:

Let’s have a look at the Syntax and understand the working of the index() in Python.

list.index(element, start, end)
  • It consists of a few parameters
    • element: This parameter specifies the element you’re searching for in terms of the index.
    • start: The index which we want to be searched.
    • end: This is an optional parameter to end the search.

Example:

Here we will take an example and check how to get the index of an element in Python List using index().

Source Code:

Country_name = ["U.S.A", "Germany", "China"]


# Searching for “Germany”
print(Country_name.index("Germany"))

In the following given code first, we will create a list and then use the list.index() function and within this function, we passed the country name which we want to be searched.

Here is the Screenshot of the following given code.

How to get the index of an element in Python List using index
How to get the index of an element in Python List using an index

As you can see in the Screenshot we have discussed how to get the index of an element in Python List using an index.

Read: Python Extend Vs Append

How to get the index of an element in Python List using enumerate()

  • In this section, we will discuss how to get the index of an element in Python List using enumerate().
  • To perform this particular task we are going to use the enumerator() function this function in Python can also be used to return the index positions of each instance of a specific element within a List.

Example:

Let’s take an example and check how to get the index of an element in Python List using enumerate().

Source Code:

new_list = [98,178,178,145,913] 
 
print ("Input list : " ,new_list) 
 
new_output = [i for i, m in enumerate(new_list) if m == 178] 
  
print ("Indices at which items 178 is present: ",new_output) 

Here is the Screenshot of the following given code

How to get the index of an element in Python List using enumerate
How to get the index of an element in Python List using enumerate

In this example, we have understood how to get the index of an element in Python List using enumerate.

Read: How to Reverse a List in Python

How to get the index of an element in Python List using filter()

  • In this section, we will discuss how to get the index of an element in Python List using filter().
  • Based on the function specified, the filter() method filters the given list. Each list element will be supplied to the function along with the necessary items, which will be filtered depending on the function’s condition.
  • To obtain the indexes for the specified element in the list, let’s utilize the filter() method.

Example:

Here we will take an example and check how to get the index of an element in Python List using filter().

Source Code:

new_vales = [78,23,17,17,89,56,17] 
print("Input list: ", new_vales)

new_result = list(filter(lambda m: new_vales[m] == 17, range(len(new_vales)))) 
print("Indexes for items 17 : ", new_result)

You can refer to the below Screenshot

How to get the index of an element in Python List using filter
How to get the index of an element in Python List using a filter()

This is how to get the index of an element in Python List using a filter().

Read: Concatenate multiple lists in Python

How to get the index of an element in Python List using for loop

  • Now let us understand how to get the index of an element in Python List using for loop.
  • We have seen that the index of the element specified as an input is provided by the list.index() method.
  • Consider the list as Cities of U.S.A = [“NewYork,” “NewYork,” California, “Florida,” “Washington,” “NewYork,”] There are three instances of the name “NewYork” in the index, and I want to find all instances of the name.
  • We should be able to obtain the numerous indexes using a for-loop.

Example:

Let’s take an example and check how to get the index of an element in Python List using for loop.

Source Code:

Cities_of_USA = ["NewYork", "NewYork", "California", "Florida", "Washington", "NewYork"] 
emp_list = [] 
for m in range(0, len(Cities_of_USA)) : 
    if Cities_of_USA[m] == 'NewYork' : 
        emp_list.append(m)
print("Input list ", Cities_of_USA)
print("Indexes for items NewYork : ", emp_list)

Here is the implementation of the following given code

How to get the index of an element in Python List using for loop
How to get the index of an element in Python List using for loop

This is how to get the index of an element in Python List using for loop.

Read: How to add string to list Python

How to get the index of an element in Python List using a while loop

  • In this section, we will discuss how to get the index of an element in Python List using more_itertools.
  • There are several useful functions included in the built-in more itertools library. The locate() function, which accepts an iterable and a function to compare against, is one of them.
  • We can use a lambda function that just determines whether an element is equal to the element we want to compare it to in order to obtain the index locations of all elements that match an element.

Example:

Let’s take an example and check how to get the index of an element in Python List using more_itertools.

Source Code:

from more_itertools import locate
new_list = [89,17,15,15,11,16,15,20,14,89]
def find_indices(new_val, new_val_2):
    new_result = locate(new_val, lambda x: x == new_val_2)
    return list(new_result)
    
print(find_indices(new_list, 15))

We created a function that accepts a list as well as the element to search for.
The function uses a lambda function to determine whether each item in the list we want to search is equal to the value we’re looking for as well as the locate() method to use the list. A list of the results is the function’s final output.

Here is the implementation of the following given code

How to get the index of an element in Python List using while loop
How to get the index of an element in Python List using a while loop

As you can see in the Screenshot we have discussed how to get the index of an element in Python List using a while loop.

You may also like to read the following Python tutorials.

In this article, we have discussed how to get the index of an element in a Python List. And also, we have covered the following given topics.

  • How to get the index of an element in Python List using list comprehension.
  • How to get the index of an element in Python List using index()
  • How to get the index of an element in Python List using enumerate()
  • How to get the index of an element in Python List using filter()
  • How to get the index of an element in Python List using for loop
  • How to get the index of an element in Python List using a while loop