All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Find Index Of All Occurrences In List

Last Updated : Mar 11, 2024

Python Find Index Of All Occurrences In List

In this tutorial we will show you the solution of python find index of all occurrences in list, when you are working as a programmer on data structures, there may be some requirements that you have to find an index of all occurrences of an element in any array or list.

It might be a little difficult to perform such a task without using the built-in functions provided by programming languages. So, let’s get into it.

Step By Step Guide On Python Find Index Of All Occurrences In List :-

Finding the indices of all occurrences of an element in a list returns a new list containing the indices at which the desired element is located.

Python provides some different methods to perform this kind of task that you can use in your Python program.

Some of the methods are described below:

  • Using range() function
  • Using the itertools module
  • Using the enumerate() function
  • Using NumPy Library
from itertools import count
import numpy as np
originalList = [1, 3, 7, 5, 4, 3]
item = 3
indexes = [i for i in range(len(originalList)) if originalList[i] == item]
print("First method : ", indexes)
zippedIndexes = [(i, j) for i, j in zip(count(), originalList) if j == item]
print("Second method : ", zippedIndexes)
indexes = [i for i, j in enumerate(originalList) if j == item]
print("Third method : ", indexes)
indexes = np.where(np.array(originalList) == item)[0]
print("Fourth method : ", indexes)
  1. In the first two lines, there are some imports that are mandatory to perform this task.
  2. The first import is the count() method from itrtools, which will help us later in this article to find an index of all occurrences of an element in a list.
  3. The next import is of the library of numpy, which will help you achieve this target.
  4. In the next line, there is a variable named originalList, which is a list and contains integer elements in it.
  5. In the next line, there is another variable named “item” which has a value of 3 assigned to it. We will use this variable as the element that we want to find from the list.
  6. In the first method, we will use Python’s built-in function called the range() function.
  7. In the next line, the range() function will get the list of all valid indices and then match the corresponding value present at each index with the given item and store the value in the indexes variable.
  8. In the next line, the print statement will display the value of the variable indexes.
  9. We will use the count() and zip() functions in the next line. The count() function will create an iterator for efficient looping to return evenly spaced values starting with the given number. The zip() function will add sequence numbers as described in the code.
  10. In the next line, the print statement will display a whole new list that contains the value of the index of all occurrences of the item.
  11. In the next line, we will use Python’s enumerate() function to find the index values of all occurrences of an element.
  12. The enumerate function will add a counter to an iterable and return the enumerated object.
  13. In the next line, the print statement will display the value of the variable indexes.
  14. And last but not least, we will use the numpy library to find the index of all occurrences of an element from the list.
  15. The numpy’s where() function returns the index positions of all items in an array that match a given value.
  16. The next, which is a print statement, will display the value of the indexes variable.

Conclusion :-

So finally, in conclusion, we can say that with the help of this tutorial, you can now find the index of all occurrences of an element in a list in a Python program.

All of the above methods that you find simple to understand will work fine.

I hope this tutorial on python find index of all occurrences in list helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪