How to Find Indices of Duplicate Items in a Python List
This guide explains how to find the indices of duplicate items within a Python list. We'll cover two main scenarios: finding all indices of a given value, and finding indices starting from a specific position within the list. We'll use enumerate() and list comprehensions for concise and efficient solutions.
Finding All Indices of a Value
The most efficient and Pythonic way to find all indices of a specific value in a list is to use a list comprehension with enumerate():
def find_indices(l, value):
return [index for index, item in enumerate(l) if item == value]
# Example Usage:
my_list = ['one', 'two', 'one', 'one']
print(find_indices(my_list, 'one')) # Output: [0, 2, 3]
print(find_indices(my_list, 'abc')) # Output: [] (empty list if not found)
enumerate(l): This function adds a counter to an iterable and returns it (as an enumerate object). It produces pairs of(index, item)for each item in the listl.for index, item in enumerate(l): This loop unpacks each pair fromenumerate()into theindexanditemvariables.if item == value: This is the filtering condition. We only keep theindexif theitemmatches thevaluewe're looking for.[index ... ]: This is the list comprehension. It builds a new list containing only theindexvalues where the condition is true.- Empty List for No Matches: If the
valueis not found in the list, the list comprehension produces an empty list ([]). This is a good, clean way to indicate no matches, and it avoids raising an exception.
Finding Indices Starting from a Specific Position
To find indices of a value, but starting the search from a particular position in the list, you can use a while loop and list.index(), but a generator function is often cleaner and more flexible:
def find_indices(l, value, start=0):
indices = []
while True:
try:
index = l.index(value, start) # Find the index
start = index + 1 # Increment start index
indices.append(index) # Append the index
except ValueError:
break # Stop if item not in the list
return indices
# Example Usage:
my_list = ['a', 'b', 'a', 'a', 'b', 'a']
print(find_indices(my_list, 'a', 1)) # Output: [2, 3, 5] (start from index 1)
print(find_indices(my_list, 'a', 3)) # Output: [3, 5] (start from index 3)
print(find_indices(my_list, 'a', 4)) # Output: [5] (start from index 4)
print(find_indices(my_list, 'a', 6)) # Output: [] (start from index 6, no more 'a's)
print(find_indices(my_list, 'x')) # Output: []
l.index(value, start): The key here is thestartparameter of theindex()method. It tellsindex()to begin searching from that position, not from the beginning of the list. If the value isn't found after thestartindex,index()raises aValueError.while Trueandtry/except ValueError: This structure handles the case where the value is not found (or no longer found after thestartindex). TheValueErrorbreaks the loop. This is a standard Python idiom for looping until a condition is met.start = index + 1: This is crucial. We updatestartto be after the index we just found, so the next call toindex()doesn't find the same item again.- The function will always return a list of indexes.