Indexes are an important concept in Python, especially when working with lists. In Python, an index is a numeric identifier that is used to get/access a specific element within a list. These indexes allow us to retrieve individual elements from a list by specifying their position using a numerical value.

This blog post discusses how to find an item’s index in a List using Python.

List Index in Python

A distinct integer value that represents each element’s location within a list is known as a list index.

Since indexes in a list begin from 0, the initial element’s index will be zero, the second one will be 1, the third one will be 2, and so on. Let’s see an example:

list_1=['Python','Java','C++','React', 'Flutter']
print(list_1[0])
print(list_1[1])

In the above code

  • The list_1 has 5 elements stored in it.
  • We have utilized the list index to get/access the index of the 0th and 1st elements in the list.

The output is as follows:

It can be seen that the 0th and 1st elements are printed using the list index.

How to Determine an Element’s Index in a List in Python?

To calculate the index of an element, different techniques are used which are given below:

  • Using index() Method
  • Using Enumerator Method

Method 1: Use the Index() Function to Determine the Index of a List Item

It is a built-in method in Python that returns the element’s index where it appears for the first time in the list. This method takes three parameters listed below.

  • Element: The element’s name that you want to search for.
  • Start: The starting index i.e., from where you wish to start the search for the element.
  • End: The ending index i.e., to where you want to stop the search for the element.

Here’s an illustration:

fields=['Data Science', 'Cloud Computing, 'Machine Learning, 'Deep Learning', 'Front-End', 'Back-End']
print("index of Machine Learning is",fields.index('Machine Learning'))
print("index of Front-End is",fields.index('Front-End',2,6))

In the above code:

  • A list is initialized which contains some string values at different indexes.
  • The second line uses the index() method to find the index (position) of the string “Machine Learning” within the fields list.
  • In the second list, additionally, two arguments are also provided which are 2,6. It searches for the “Front-End” between the second index and the sixth index.

The output of the code is shown below:

It can be seen that the index of Machine Learning is 2 and the index of Front-End is 4.

Method 2: Use Enumerate() to Find the Index of a List Item

The stated method will retrieve all the indexes/positions at which a specific element is located in a Python List. Let’s do this method practically with an example.

list1=['Data Science', 'Cloud Computing, 'Python','Machine Learning', 'Deep Learning', 'Front-End', 'Python', 'back-End','Python']
index = [i for i ,z in enumerate(list1) if z == 'Python']
print("Index for Value Python: ", index)

In the above code:

  • A list is initialized which contains some string values at different indexes.
  • In the second line, the enumerate() method is used to loop over the elements of list1 along with their respective indices. For each pair, it checks if the value e is equal to the string Python using the condition if z== Python. If the condition evaluates to True, the index i is added to the list.
  • Finally, the code will print the index list that contains the indices where “Python” appears in the “list1”.

The output of the code is shown below:

It can be seen that the program returned the indexes of the string “Python”.

Conclusion

A unique integer value that represents the position of each element in a list is known as the list index.

We can calculate an element’s index in a list using two methods which are index() and enumerate(). index() retrieves the index of the element’s first appearance in the list whereas the enumerate() method returns all the indexes/positions at which a specific element is located in Python.