Python - Duplicate Element Indices in List Last Updated : 01 Feb, 2025 Comments Improve Suggest changes Like Article Like Report 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 dictionaryWe 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.CounterWe 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]}. Comment More infoAdvertise with us Next Article Python - Duplicate Element Indices in List manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads 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 Remove an Element from a List by Index in Python We are given a list and an index value. We have to remove an element from a list that is present at that index value in a list in Python. In this article, we will see how we can remove an element from a list by using the index value in Python. Example: Input: [1, 2, 3, 4, 5], index = 2 Output: [1, 2 3 min read Python - Check if all elements in List are same To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. Pythona = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) OutputTrue Explanation:Converting 3 min read List Product Excluding Duplicates - Python The task of finding the product of unique elements in a list involves identifying and multiplying only the distinct values, effectively excluding any duplicates. For example, if a = [1, 3, 5, 6, 3, 5, 6, 1], the unique elements would be {1, 3, 5, 6}, and the product of these values would be 90. Usin 3 min read Find Index of Element in Array - Python In Python, arrays are used to store multiple values in a single variable, similar to lists but they offer a more compact and efficient way to store data when we need to handle homogeneous data types . While lists are flexible, arrays are ideal when we want better memory efficiency or need to perform 2 min read Like