Find the indices of duplicate items in a List in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# Find the indices of duplicate items in a List in Python

To find the indices of duplicate items in a list:

  1. Use a list comprehension to iterate over the list with enumerate().
  2. Check if each item is equal to the given value.
  3. Return the index of the matching items.
main.py
def find_indices(l, value): return [ index for index, item in enumerate(l) if item == value ] # ๐Ÿ‘‡๏ธ [0, 2, 3] print(find_indices(['one', 'two', 'one', 'one'], 'one')) # ๐Ÿ‘‡๏ธ [] print(find_indices(['one', 'two', 'one', 'one'], 'abc'))

find indices of duplicate items in list

The code for this article is available on GitHub
If you need to specify a start index, scroll down to the next subheading.

We used the enumerate() function to get access to the index of the current iteration.

The enumerate() function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.

main.py
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): print(index, item) # ๐Ÿ‘‰๏ธ 0 bobby, 1 hadz, 2 com

On each iteration, we check if the current item is equal to the given value.

main.py
def find_indices(l, value): return [ index for index, item in enumerate(l) if item == value ]

If the condition is met, we return the corresponding index.

The list the function returns contains all indices of the value in the original list.

# Find the indices of duplicate items in a List with start index

If you need to find the indices of the duplicate items in a list with a start index:

  1. Use a while True loop to iterate until the occurrences of the item are found.
  2. Use the list.index() method to get the index of each occurrence by specifying a start index.
  3. Append the matching indices to a new list.
main.py
def find_indices(l, value, start=0): indices = [] while True: try: index = l.index(value, start) start = index + 1 indices.append(index) except ValueError: break return indices # ๐Ÿ‘‡๏ธ [2, 3, 5] print(find_indices(['a', 'b', 'a', 'a', 'b', 'a'], 'a', 1)) # ๐Ÿ‘‡๏ธ [3, 5] print(find_indices(['a', 'b', 'a', 'a', 'b', 'a'], 'a', 3)) # ๐Ÿ‘‡๏ธ [5] print(find_indices(['a', 'b', 'a', 'a', 'b', 'a'], 'a', 4)) # ๐Ÿ‘‡๏ธ [] print(find_indices(['a', 'b', 'a', 'a', 'b', 'a'], 'a', 6))

find indices of duplicates with start index

The code for this article is available on GitHub

The function takes an optional start index.

The start index is the index at which we start looking for occurrences of the given value in the list.

The list.index() method returns the index of the first item whose value is equal to the provided argument.

The list.index() method takes an optional `start` argument and starts looking for the specified value from the start index onwards.
main.py
print(['a', 'b', 'c', 'a'].index('a', 1)) # ๐Ÿ‘‰๏ธ 3

The method raises a ValueError if there is no such item in the list.

If a ValueError is raised, we break out of the while loop and return the indices list.

main.py
def find_indices(l, value, start=0): indices = [] while True: try: index = l.index(value, start) start = index + 1 indices.append(index) except ValueError: break return indices
The code for this article is available on GitHub

Otherwise, we increment the start index by 1 and append the index to the indices list.

The list.append() method adds an item to the end of the list.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev