Python find index of all occurrences in list


In this article, we are going to look at 5 different methods to find the index of all occurrences of an item in a list in Python with examples.

    Table of Contents

  1. Using list comprehension
  2. Using enumerate()
  3. Using for loop
  4. Using while loop
  5. Using list.index()
  6. Conclusion

1. Using list comprehension

List comprehension is powerful tool, it can help us to find the index of all occurrences of an item in a list in Python.

Let's see how to do it.

Let's say we have a list fruits and we want to find the index of all occurrences of an item 'apple' in the list.

Here is the code:

fruits = ['apple', 'banana', 'orange', 'apple', 'pear']

# find all indexes of 'apple' in the list
indexes = [i for i, x in enumerate(fruits) if x == 'apple']

print(indexes) # [0, 3]

Inside the list comprehension, we are using enumerate() function to get the index and value of each item in the list and then we are checking if the value is equal to 'apple' or not.

If the value is equal to 'apple', then we are appending the index to the indexes list.


2. Using enumerate()

enumerate() function returns an enumerate object. It contains the index and value of all the items in the list as a tuple.

Using this enumerate object, we can find the index of all occurrences of an item in a list in Python.

Here is how we can do this:

fruits = ['apple', 'banana', 'orange', 'apple', 'pear']

# loop through the enumerate object
indexes = []
for i, x in enumerate(fruits):
    if x == 'apple':
        indexes.append(i)

print(indexes) # [0, 3]

Here, we are looping through the enumerate object and checking if the value is equal to 'apple' or not.

If the value is equal to 'apple', then we are appending the index to the indexes list.


3. Using for loop

For loop is a multi-purpose tool in Python, we can use it to solve many problems.

To find all the indexes of an item in a list, we can loop for number of times equal to the length of the list and check if the value at each index is equal to the item or not.

The code below shows its implementation:

fruits = ['apple', 'banana', 'orange', 'apple', 'pear']

# loop through the list
indexes = []
for i in range(len(fruits)):
    if fruits[i] == 'apple':
        indexes.append(i)

print(indexes) # [0, 3]

4. Using while loop

While loop is another type of loop in Python.

It can be used in similar way as for loop to find the index of all occurrences of an item in a list in Python.

Here is the code:

fruits = ['apple', 'banana', 'orange', 'apple', 'pear']

# loop through the list using while loop
indexes = []
# initial index
i = 0
while i < len(fruits):
    if fruits[i] == 'apple':
        indexes.append(i)
    i += 1

print(indexes)

5. Using list.index()

list.index() method returns the index of the first occurrence of the item in the list.

It can also be modified to return first occurrence of the item after a given index (by passing start index as an argument).

using this property, we can find all the indexes of an item in a list on after another.

Here is the code:

fruits = ['apple', 'banana', 'orange', 'apple', 'pear']

# using list.index() method
indexes = []
i = 0
while i < len(fruits):
    try:
        index = fruits.index('apple', i)
        indexes.append(index)
        i = index + 1
    except ValueError:
        break

print(indexes) # [0, 3]

For more detail learning visit Python: Get Index of Item in List.


Conclusion

In this tutorial, we have learned 5 different ways to find all the indexes of an item in a list in Python.

Using list comprehension is the most efficient way to find all the indexes of an item in a single line.

Happy Learning!😇