Python - Duplicate Element Indices in List
Last Updated :
12 Jul, 2025
We are having a list we need to find the duplicate element indices. For example, we are given a list a = [10, 20, 30, 20, 40, 30, 50] we need to find indices of the duplicate items so that output should be {20: [1, 3], 30: [2, 5]}.
Using a loop and a dictionary
We iterate through the list using enumerate() and store indices of each element in a dictionary appending new indices if element appears again.
Python
a = [10, 20, 30, 20, 40, 30, 50]
d = {}
for i, num in enumerate(a):
if num in d:
d[num].append(i)
else:
d[num] = [i]
# Filter to keep only the numbers with more than one occurrence
ans = {key: value for key, value in d.items() if len(value) > 1}
print(ans)
Output{20: [1, 3], 30: [2, 5]}
Explanation:
- Loop iterates through list using enumerate() storing indices of each element in a dictionary d appending indices if element appears more than once.
- Final dictionary comprehension filters d to retain only elements with multiple occurrences.
Using collections.Counter
We use Counter to count occurrences of each element in the list and then create a dictionary that maps duplicate elements to their indices using list comprehension.
Python
from collections import Counter
a = [10, 20, 30, 20, 40, 30, 50]
# Count occurrences of each element in the list
c = Counter(a)
# Create a dictionary mapping duplicate elements to their indices
ans = {num: [i for i, x in enumerate(a) if x == num] for num, cnt in c.items() if cnt > 1}
print(ans)
Output{20: [1, 3], 30: [2, 5]}
Explanation:
- Counter(a) counts the occurrences of each element and then a dictionary comprehension extracts indices of elements appearing more than once using enumerate().
- Final dictionary stores duplicate elements as keys and their corresponding indices as values.
Using list comprehension
We use list comprehension to create a dictionary where each duplicate element maps to a list of its indices filtering elements that appear more than once. This is achieved by iterating over unique elements and using enumerate() to collect indices of duplicates.
Python
a = [10, 20, 30, 20, 40, 30, 50]
seen = set()
# Create a dictionary mapping duplicate elements to their indices
ans = {num: [i for i, x in enumerate(a) if x == num] for num in set(a) if a.count(num) > 1}
print(ans)
Output{20: [1, 3], 30: [2, 5]}
Explanation:
- We use a dictionary comprehension to iterate over unique elements in a (using set(a)) and check if each element appears more than once (a.count(num) > 1).
- For each duplicate element we use enumerate(a) to collect all its indices creating a dictionary where keys are duplicate elements and values are lists of their indices.
Using a loop and list.count()
We iterate through list and use list.count() to check if an element appears more than once then store its indices in a dictionary if it's not already processed. This ensures we collect indices of duplicate elements while avoiding redundant checks.
Python
a = [10, 20, 30, 20, 40, 30, 50]
d = {}
for i, num in enumerate(a):
if a.count(num) > 1 and num not in d:
# Add the element to the dictionary with a list of its indices
d[num] = [j for j, x in enumerate(a) if x == num]
print(d)
Output{20: [1, 3], 30: [2, 5]}
Explanation:
- Loop iterates through list checking if each element appears more than once (a.count(num) > 1) and ensures that it is added only once to dictionary d.
- For each duplicate element a list comprehension collects all indices where element occurs storing them in d as {element: [indices]}.
Similar Reads
Remove elements at Indices in List - Python In Python, lists store items in a specific order and each item has an index. Removing elements by index means creating a new list that excludes items at certain positions. This helps in keeping only the needed elements while discarding others based on their index. For example:Input : li= [5, 6, 3, 7
2 min read
Python - Indices of atmost K elements in list Many times we might have problem in which we need to find indices rather than the actual numbers and more often, the result is conditioned. First approach coming to mind can be a simple index function and get indices less than or equal than particular number, but this approach fails in case of dupli
7 min read
Group Elements at Same Indices in a Multi-List - Python We are given a 2D list, we have to group elements at the same indices in a multi-list which means combining elements that are positioned at identical indices across different list. For example:If we have a 2D list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] then grouping elements at the same indices would re
4 min read
How to Find Duplicates in a List - Python Finding duplicates in a list is a common task in programming. In Python, there are several ways to do this. Letâs explore the efficient methods to find duplicates. Using a Set (Most Efficient for Large Lists)Set() method is used to set a track seen elements and helps to identify duplicates. Pythona
2 min read
Python - Index Directory of Elements Given a list of elements, the task is to write a python program to compute all the indices of all the elements. Examples: Input: test_list = [7, 6, 3, 7, 8, 3, 6, 7, 8] Output: {8: [4, 8], 3: [2, 5], 6: [1, 6], 7: [0, 3, 7]} Explanation: 8 occurs at 4th and 8th index, 3 occurs at 2nd and 5th index a
3 min read
Python program to remove duplicate elements index from other list Given two lists, the task is to write a Python program to remove all the index elements from 2nd list which are duplicate element indices from 1st list. Examples: Input : test_list1 = [3, 5, 6, 5, 3, 7, 8, 6], test_list2 = [1, 7, 6, 3, 7, 9, 10, 11] Output : [1, 7, 6, 9, 10] Explanation : 3, 7 and 1
7 min read