Find Index of an Element in a List in Python



In Python, an index is an element's location within an ordered list. Where n is the length of the list, and the first entry has a zero index, and the last element has an n-1 index.

In this tutorial, we will look at how to find out the index of an element in a list in Python. There are different methods to retrieve the index of an element.

Using index() method

Three arguments can be passed to the list index() method in Python -

  • element: element that has to be found.
  • start (optional): Begin your search using this index.
  • end (optional): search up to this index for the element.

Example

This method allows the user to search for an element in the list within the boundaries (start and end). In this example, we looked at two cases -

  • Case 1: Using the start parameter to search for an element from index 0.
  • Case 2: Fetching an element's index outside the specified boundaries results in an error.
lst=['tutorials', 'point', 'easy', 'learning', 'simply']
index1 = lst.index('easy',0)
print(index1)
index2=lst.index('simply',1,3)
print(index2)

On executing the above program, the following output is obtained -

2
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    index2=lst.index('simply',1,3)
ValueError: 'simply' is not in list

Using List Comprehension and enumerate() function

List comprehension in Python is an approach for creating a new list from an old one. Using list comprehension, the following is how you would obtain all indices for each instance of the string "Program".

Example

The enumerate() function in Python allows you to keep the item indices that satisfy the given condition. For each item in the list (animal_names) that is given to the function as an argument, it first provides a pair (index, item). The list item itself is referred to as an item, whereas the index refers to the list item's index number.

Then, it performs the function of a counter, picking and shifting the indices of the items that fit your condition. The counter starts counting at 0 and increments each time the condition you set is met.

animal_names= ["Elephant","Lion","Zebra","Lion","Tiger","Cheetah"]
indices = [index for (index, item) in enumerate(animal_names) if item == "Lion"]
print('The indices of the given item Lion is:,',indices)

A new list is formed with all the indices of the text "Lion" when the list comprehension is used as shown in the below output -

The indices of the given item Lion is:, [1, 3]

Using for loop

In this example code, we have used a simple 'for' loop to iterate throughout the list count and find the required element(key) by counting for each iteration. If the element is not found in the list, then the below code will print that the element is not found.

lst=['tutorials','point','easy','learning','simply']
key= 'easy'
count=0
for i in lst:
   if(key==i):
      count=count+1
      print(count+1)
      break
   else:
      count=count+1
if (count==len(lst)):
   print('Element not found')

The following output is produced when the program mentioned above is executed -

4

Using a Function

In this program, we will create a reusable function that either returns the index of the element or, if it cannot be found, it will return a message.

# Function to find index of an element
def find_index(lst, element):
    try:
        return lst.index(element)
    except ValueError:
        return f"{element} is not in the list."

# Define a list of animals
animals = ['dog', 'cat', 'rabbit']

# Test the function
print(find_index(animals, 'cat'))
print(find_index(animals, 'lion')) 

This will create the following outcome -

2
lion is not in the list.
Updated on: 2025-04-24T11:46:46+05:30

614 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements