How to find what is the index of an element in a list in Python?


Index in python refers to the position of the element within an ordered list. The starting element is of zero index and the last element is of n 1 index where n is the length of the list.

In this tutorial, we will look at how to find out the index of an element in a list in Python. They 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 user to search an element in the list within the boundaries (start and end). In this example, we looked at two cases −

Case 1 − Using start parameter to search for an element from an index 0.

Case 2 − Fetching an elements index outside the specified boundaries which 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)

Output

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 item, whereas 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)

Output

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')

Output

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

4

Updated on: 08-Nov-2022

243 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements