Python

Python Finds the Index of All Occurrences in a List

A list is utilized in Python to keep numerous elements under a single variable. The lists are an organized group of elements. Each element has its distinctive place, and each element can be obtained by using its position in the list. These positions are called indexes. An element can be present at different positions in a list. In this tutorial, we will enlighten on how to find the indices of all items in a list. The index of a list starts with 0. This means that the first item in the list has an index of 0, not 1, and then, the second item has an index of 1.

Now, let’s see how to check the index of an element in Python’s list.

Utilization of the list.index() Method:

The index() is an in-built method of Python. For the implementation of Python code, we have installed the Spyder software (version 5). Next, we create a new project by tapping the “new file” option from the menu bar. The new file we created is named “temp4.py”.

In this program, we use the index() method of the list with the items passed as arguments. The items are birds’ names. Here bird_to_find defines the bird whose index we want to find in the list:

We take a List of birds. By using the index() method, we have to know the index of the bird “turkey” in the list. The index() method responds to an integer that indicates the index of the first match of bird in the List.

Now, we have to run that code by hitting the “F5” button from the keyboard. The index of a list begins with 0. This means that the first item in the list has an index of 0, not 1. Then, the second item has an index of 1. So, the bird “turkey” is present at 6th position, and its index is 5:

Finding Index of Element in List by Using Start and End:

We find the index of any element in the list by the use of start and end. In this example, we make a list containing numbers. Using the index() function, we will verify the index of the number “16” in the list. Then, we pass start and end. The index() function observes only those items in the list starting from the start index till the end position in list_of_elements.

We give the 1st and last positions of the List, where the exploration has to occur in the list. The start is an optional parameter and has an assigned value of 3. It shows the start index to find the number. The end is also an optional parameter. It serves as the stop index to determine the element. If not specified the end index, it is observed till the end of the list:

The index of a list starts with 0. This means that the first item in the list has an index of 0, not 1. Then, the second item has an index of 1. The number “16” is present at the 5th position, so its index is 4:

Finding the Indexes of the Element by Utilization of enumerate() Function:

The enumerate() function can be utilized here. This function adds an index number to an iterable and returns an index. In this code, list_of_birds represent the iterable object as it supports iteration:

We use the enumerate() function here. The bird “kite” is present at different positions in the list. The index of bird “kite” is 0, 3 ,4, 7.

Error Occurrence (Element Is Not Present):

We have made a list and will try to notice the index of an item not on the list. Since index() can return ValueError, use Python TryExcept while operation index(). If the item we are seeing is not shown on the list, we will catch a ValueError.

As the number “23” is not found in the list, so ValueError is returned.

Conclusion:

In this guide, we deliberated over numerous approaches for finding the index of items in the list in Python. We see how to acquire the index of the element in the list. The index of the Python lists is utilized to get items in Python. So, follow this guide whenever you are working on the list’s index.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content