# Find Index of Duplicate Elements in List in Python (2 Examples)

Hi! This short tutorial will show you how to get the index of duplicate elements in a list in the Python programming language.

Here is an overview:

Letâ€™s get into the Python code!

## Create Example List

We will here create the example Python list of integers, the index of whose duplicate elements we will determine in this tutorial.

So, in your Python coding IDE, run the code below to create the example list:

```my_list = [1, 2, 3, 4, 2, 5, 6, 4]

print(my_list)

# [1, 2, 3, 4, 2, 5, 6, 4]

print(type(my_list))

# <class 'list'>```

Now that we have created the example list, we will consider two ways to return the index of the duplicate elements in the list.

## Example 1: Get Index of Duplicate Elements in List Using for Loop & enumerate() Function

In this first example, we will use a for loop along with the enumerate() function to determine the index of the duplicate elements in the list:

```indexes = []

for i, v in enumerate(my_list):
if my_list.count(v) > 1 and i not in indexes:
indexes.append(i)

print(indexes)

# [1, 3, 4, 7]

print(type(indexes))

# <class 'list'>```

Here, we use a for loop along with the `enumerate()` function to iterate through the elements of the list along with their corresponding indices.

We first initialize an empty list called `indexes` to store the indices of the duplicate elements.

Inside the loop, we check if the current element occurs more than once in the list by using the count() method.

If it does and the current index is not already in `indexes`, we append the index to `indexes`.

Finally, we use the print() function to print `indexes`, which contains the indices of the duplicate elements in the given list.

## Example 2: Get Index of Duplicate Elements in List Using collections Module

In this next example, we will use Pythonâ€™s built-in collections module to obtain the index of the duplicate elements in the list:

```from collections import Counter

counts = Counter(my_list)

indexes = [index for index, value in enumerate(my_list) if counts[value] > 1]

print(indexes)

# [1, 3, 4, 7]

print(type(indexes))

# <class 'list'>```

In this example, we first import the Counter class from the collections module.

Next, we create an instance of the Counter class called `counts` by passing `my_list` as an argument. The Counter class provides a convenient way to count the occurrences of elements in a collection.

We then use a list comprehension to iterate through `my_list` using the `enumerate()` function and checking if the count of the current value in `my_list` (obtained from `counts[value]`) is greater than 1.

If it is, the index is appended to `indexes`.

Finally, we print the `indexes` list, which contains the indices of the duplicate elements in the given list.

## Video, Further Resources & Summary

Do you need more explanations on how to find the index of duplicate elements 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 duplicate elements in a list in Python.

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown, using two examples, how to find the index of duplicate elements in a list in Python. Your use case will determine which method to adopt.

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