Find Index of Same Element in List in Python (2 Examples)
Hi! This tutorial will show you how to get the index of the same element in a list in the Python programming language.
Here is a quick overview:
Let’s get coding!
Create Demo List & Select Reference Element
Here, we will create a demo Python list of integers that we will use in the examples in this tutorial. Therefore, in your preferred Python programming IDE, run the line of code below to create the demo list of integers:
my_list = [1, 2, 3, 2, 4, 5, 2] print(my_list) # [1, 2, 3, 2, 4, 5, 2] print(type(my_list)) # <class 'list'>
We will opt for the most occurring element in the list as a reference element. In order to find it, we need to install and import Python’s NumPy library to use its relevant functions:
# install NumPy pip install NumPy # import NumPy import numpy as np
Now that we have installed and imported NumPy into our Python environment, we will use a couple of its functions to get the most frequently occurring element in the list.
counts = np.bincount(my_list) most_common_element = np.argmax(counts) print(most_common_element) # 2
Now, it is time to find the same element occurring in the list in the examples!
Example 1: Get Index of Same Element in List Using For Loop
In this example, we will use a for loop to iterate through the length of the list and return the indexes of the same element:
indexes =  for i in range(len(my_list)): if my_list[i] == most_common_element: indexes.append(i) print(indexes) # [1, 3, 6] print(type(indexes)) # <class 'list'>
In the above example, we used a conditional “if” statement inside the for loop to check if there are elements in the list that are equal (denoted by the double equals sign ==) to the most common element. If yes, it will return the indexes of each element in a list.
Example 2: Get Index of Same Element in List Using List Comprehension & enumerate() Function
indexes = [i for i,x in enumerate(my_list) if x == most_common_element] print(indexes) # [1, 3, 6] print(type(indexes)) # <class 'list'>
enumerate() function returns a list of tuples containing the elements in the list and their indexes in the form of (index, element). The list comprehension iterates through the output of the
enumerate() function, and if there are elements that are exactly equal to the most common element, then it will return their indexes.
We could have hard-coded the most common element into both example solutions, just by looking at the list, finding the most frequently occurring element, and parsing that to our solutions. However, doing so will not make our solution very robust and flexible.
With that, we have demonstrated how to find the index of the same element in a list in Python. I hope you found this tutorial helpful!
Video, Further Resources & Summary
Do you need more explanations on how to find the index of the same element in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to find the index of the same element in a list in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Append to 2D List in Python (3 Examples)
- Transpose 2D List in Python (3 Examples)
- Convert List of Tuples to List of Lists in Python (3 Examples)
- count() Method for Lists in Python (2 Examples)
- Learn Python Programming
This post has shown how to find the index of the same element in a list in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter