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. Show
Method #1 : Using loop + set()
Output : 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
Output : The original list is : [1, 4, 5, 5, 5, 9, 1] The list of duplicate elements is : [3, 4, 6] Article Tags : Python Python Programs Python list-programs Python: Get index of item in ListTo find index of element in list in python, we are going to use a function list.index(), list.index() Python’s list data type provides this method to find the first index of a given element in list or a sub list i.e. Advertisements list.index(x[, start[, end]])Arguments :
Returns: A zero based index of first occurrence of given element in the list or range. If there is no such element then it raises a ValueError. Important Point : list.index() returns the index in a 0 based manner i.e. first element in the list has index 0 and second element in index is 1. Let’s use this function to find the indexes of a given item in the list, Suppose we have a list of strings, # List of strings list_of_elems = ['Hello', 'Ok', 'is', 'Ok', 'test', 'this', 'is', 'a', 'test', 'Ok']Now let’s find the index of the first occurrence of item ‘Ok‘ in the list, elem = 'Ok' # Find index position of first occurrence of 'Ok' in the list index_pos = list_of_elems.index(elem) print(f'First Index of element "{elem}" in the list : ', index_pos)Output First Index of element "Ok" in the list : 1As in the list.index() we did not provided start & end arguments, so it searched for the ‘Ok‘ in the complete list. But returned the index position as soon as it encountered the first occurrence of ‘Ok‘ in the list. But if searched item doesn’t exists in the list, then index() will raise ValueError. Therefore we need to be ready for this kind of scenario. For example, list_of_elems = ['Hello', 'Ok', 'is', 'Ok', 'test', 'this', 'is', 'a', 'test', 'Ok'] elem = 'Why' try: index_pos = list_of_elems.index(elem) print(f'First Index of element "{elem}" in the list : ', index_pos) except ValueError as e: print(f'Element "{elem}" not found in the list: ', e)Output Element "Why" not found in the list: 'Why' is not in listAs ‘Why‘ was not present in the list, so list.index() raised ValueError. Find an index of duplicate elements in list Python ExampleSimple example code. Output: Another example using collections You have to import the collections module for this example. import collections original = [1, 2, 3, 2, 1, 5, 6, 5, 5, 5] dup = [item for item, count in collections.Counter(original).items() if count > 1] for i in dup: print("index of", i, "=", original.index(i))Output: index of 1 = 0 How do I get the index of a duplicated string in a list?Traversing through the length of the list so that we can keep a track of each index and print the index. import collections original = ['A', 'B', 'A', 'B', 'C'] dup = [item for item, count in collections.Counter(original).items() if count > 1] for i in dup: print("index of", i, "=", original.index(i))Output: index of A = 0 Do comment if you have any questions and doubts or suggestions on this Python list topic.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical. Share this:
RelatedPython List index()The list index() method helps you to find the first lowest index of the given element. If there are duplicate elements inside the list, the first index of the element is returned. This is the easiest and straightforward way to get the index. Besides the built-in list index() method, you can also use other ways to get the index like looping through the list, using list comprehensions, enumerate(), filter methods. The list index() method returns the first lowest index of the given element. Syntaxlist.index(element, start, end)Parameters
Return ValueThe list index() method returns the index of the given element. If the element is not present in the list, the index() method will throw an error, for example, ValueError: ‘Element’ is not in the list. Example: To find the index of the given element.In the list my_list = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’] , we would like to know the index for element C and F. The example below shows how to get the index. my_list = ['A', 'B', 'C', 'D', 'E', 'F'] print("The index of element C is ", my_list.index('C')) print("The index of element F is ", my_list.index('F'))Output: Example: Using start and end in index()In this example will try to limit searching for index in a list using start and end index. my_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'] print("The index of element C is ", my_list.index('C', 1, 5)) print("The index of element F is ", my_list.index('F', 3, 7)) #using just the startindex print("The index of element D is ", my_list.index('D', 1))Output: The index of element C is 2 The index of element F is 5 The index of element D is 3Example: To test index() method with an element that is not present.When you try to search for index in the list for element that is not present ,you will get an error as shown below: my_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'] print("The index of element C is ", my_list.index('Z'))Output: Traceback (most recent call last): File "display.py", line 3, in <module> print("The index of element C is ", my_list.index('Z')) ValueError: 'Z' is not in list1. Using index() functionA simple solution is to get iterate through the list with indices using list comprehension and check for another occurrence of each encountered element using the index() function. The time complexity of this solution would be quadratic, and the code does not handle repeated elements in output.
DownloadRun Code “is there any function who finds the duplicate and returns the index number of duplicate element in list in python” Code Answer’scount the duplicates in a list in python python by Repulsive Raven on Jan 30 2021 Comment 8 find duplicated entries present in a list whatever by Troubled Thrush on Jul 09 2021 Comment 0 Add a Grepper Answer Python answers related to “is there any function who finds the duplicate and returns the index number of duplicate element in list in python”
Python queries related to “is there any function who finds the duplicate and returns the index number of duplicate element in list in python”
|