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]
# [1, 2, 3, 2, 4, 5, 2]
# <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)
# 2

The np.bincount() function counts the number of times each element occurs in the list, while np.argmax() returns the most common element, which in this case is 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:
# [1, 3, 6]
# <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

In this second example, we will use list comprehension and Python’s enumerate() function to get the index of the same element in the list:

indexes = [i for i,x in enumerate(my_list) if x == most_common_element]
# [1, 3, 6]
# <class 'list'>

The 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:

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.


R & Python Expert Ifeanyi Idiaye

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.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.