In this article we will discuss different ways to find indexes of all occurrences of an item in the list. Also a way to find indexes of items in a list that satisfy a certain condition.

## Find index of first occurrence of an item in the list

To find indexes of an item in a list, we are going to use a function i.e.

list.index()

Python’s list data type provides this method to find the first index of a given element in list or a sub list i.e.

Arguments :

• x : Item to be searched in the list
• start : If provided, search will start from this index. Default is 0.
• end : If provided, search will end at this index. Default is the end of list.

Returns: A zero based index of first occurrence of given element in the list or range. If there is no such element then it raises a ValueError.

Important Point : list.index() returns the index in a 0 based manner i.e. first element in the list has index 0 and second element in index is 1.

Let’s use this function to find the indexes of a given item in the list,

Suppose we have a list of strings,

Now let’s find the index of the first occurrence of item ‘Ok‘ in the list,

Output

As in the list.index() we did not provided start & end arguments, so it searched for the ‘Ok‘ in the complete list. But returned the index position as soon as it encountered the first occurrence of ‘Ok‘ in the list.

But if searched item doesn’t exists in the list, then index() will raise ValueError. Therefore we need to be ready for this kind of scenario. For example,

Output

As ‘Why‘ was not present in the list, so list.index() raised ValueError.

Till now we have seen, how to find index of first occurrence of an item in the list. But what if there are multiple occurrences of an item in the list and we want to know their indexes ? Let’s see how to do that,

## Find indexes of all occurrences of an item in list

Suppose we have a list of strings i.e.

Now we want to find indexes of all occurrences of ‘Ok’ in the list, like this,

They are differences ways to do that, let’s see them one by one.

#### Find all indexes of an item in list using list.index()

As list.index() returns the index of first occurrence of an item in list. So, to find other occurrences of item in list, we will call list.index() repeatedly with range arguments. We have created a function that uses list.index() and returns a list of indexes of all occurrences of an item in given list i.e.

This function calls the list.index() in a loop. Initially it looks for item from 0th index in list. But when it encounters the item, then from next iteration it looks from that location onward till the end of list is reached. It keeps the indexes of matched elements in a separate list and returns that in the end.

Now let’s use above function to find all indexes of ‘Ok’ in the list,

Output

#### Find all indexes of an item in list directly while iterating

Instead of using list.index() function, we can directly iterate over the list elements by indexes using range() function. Then for each element it checks,
if it matches with our item or not. If yes then keep its index in list i.e.

Output

Simple solution. But let’s look into some single line alternatives,

#### Use List Comprehension to find all indexes of an item in list

Concept is similar but instead of using for loop for iteration we can achieve same using list comprehension i.e.

Output

#### Use more_itertools.locate() to find all indexes of an item in list

locate() yields the index of each item in list for which given callback returns True.

To find all the occurrences of item ‘Ok‘ in the list we will pass a lambda function to locate(), that will check if item is ‘Ok‘ or not i.e.

Output

locate() yields the indexes of elements in the list for which given predicate is True. So, in our case it yields the indexes of ‘Ok’ in the list. We collected those indexes in a list.

## Find indexes of items in a list that satisfy certain conditions

Suppose we want to find indexes of items in a list that satisfy certain condition like indexes of items in a list of strings whose length is less than 3.
To do that we have created a generic function i.e.

This function accepts a list of elements and a callback function as arguments. For each element it calls the callback function and if callback() function returns True then it keeps the index of that element in a new list. In the end it returns the indexes of items in the list for which callback() returned True.

Let’s call this function to find indexes of items in list of strings whose length are less than 3 i.e.

Output

Complete example is as follows,

Output:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.