Python | Duplicate element indices in list
While working with Python list, sometimes, we require to check for duplicates and may also sometimes require to track their indices. This kind of application can occur in day-day programming. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using loop + set()
This task can be solved using the combination of above functions. In this, we just insert all the elements in set and then compare each element’s existence in actual list. If it’s the second occurrence or more, then index is added in result list.
# Python3 code to demonstrate working of # Duplicate element indices in list # Using set() + loop # initializing list test_list = [1, 4, 5, 5, 5, 9, 1] # printing original list print("The original list is : " + str(test_list)) # Duplicate element indices in list # Using set() + loop oc_set = set() res = [] for idx, val in enumerate(test_list): if val not in oc_set: oc_set.add(val) else: res.append(idx) # printing result print("The list of duplicate elements is : " + str(res)) |
The original list is : [1, 4, 5, 5, 5, 9, 1] The list of duplicate elements is : [3, 4, 6]
Method #2 : Using list comprehension + list slicing
This method is one liner alternative to perform this task. In this we just check for the non repeating element using list slicing and keep adding index in case it’s repeated.
# Python3 code to demonstrate working of # Duplicate element indices in list # Using list comprehension + list slicing # initializing list test_list = [1, 4, 5, 5, 5, 9, 1] # printing original list print("The original list is : " + str(test_list)) # Duplicate element indices in list # Using list comprehension + list slicing res = [idx for idx, val in enumerate(test_list) if val in test_list[:idx]] # printing result print("The list of duplicate elements is : " + str(res)) |
The original list is : [1, 4, 5, 5, 5, 9, 1] The list of duplicate elements is : [3, 4, 6]
Recommended Posts:
- Python | Indices list of matching element from other list
- Python | Indices of sorted list of list elements
- Python | Ways to find indices of value in list
- Python | Find indices with None values in given list
- Python | Find elements of a list by indices
- Python | Indices of N largest elements in list
- Python | Group elements at same indices in a multi-list
- Python | Get indices of True values in a binary list
- Python program to fetch the indices of true values in a Boolean list
- Python | Removing duplicate dicts in list
- Python | Duplicate substring removal from list
- Python | Altering duplicate values from given list
- Python | Remove unordered duplicate elements from a list
- Python | Replace elements in second list with index of same element in first list
- Python | Remove tuples having duplicate first value from given list of tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



