Python program to remove duplicate elements index from other list - GeeksforGeeks

13 Sep, 2021
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 11 correspond to 2nd occurrence of 5, 3 and 6, hence removed.
Input : test_list1 = [3, 5, 6, 5, 3, 7, 8], test_list2 = [1, 7, 6, 3, 7, 9, 10] Output : [1, 7, 6, 9, 10] Explanation : 3 and 7 correspond to 2nd occurrence of 5 and 3 hence removed.  
Method 1: Using list comprehension + loop + enumerate()
In this, we perform task of getting all the indices using enumerate() and loop using set to store already occurred elements. Then, same indices from other list are omitted.
Python3
# Python3 code to demonstrate working of# Remove duplicate elements index from other list# Using list comprehension + loop + enumerate()  # initializing liststest_list1 = [3, 5, 6, 5, 3, 7, 8, 6]test_list2 = [1, 7, 6, 3, 7, 9, 10, 11]               # printing original listsprint("The original list 1 is : " + str(test_list1))print("The original list 2 is : " + str(test_list2))  oc_set = set() temp = []   # getting duplicate elements listfor idx, val in enumerate(test_list1):     if val not in oc_set:         oc_set.add(val)              else:         temp.append(idx)  # excluding duplicate indices from other listres = [ele for idx, ele in enumerate(test_list2) if idx not in temp]  # printing resultprint("The list after removing duplicate indices : " + str(res))
 Output:
The original list 1 is : [3, 5, 6, 5, 3, 7, 8, 6]
The original list 2 is : [1, 7, 6, 3, 7, 9, 10, 11]
The list after removing duplicate indices : [1, 7, 6, 9, 10]
 Method 2: Using list comprehension + enumerate()
In this, we perform similar task as above just in a shorthand manner, extracting indices using enumerate() and list comprehension.
Python3
# Python3 code to demonstrate working of# Remove duplicate elements index from other list# Using list comprehension + enumerate()  # initializing liststest_list1 = [3, 5, 6, 5, 3, 7, 8, 6]test_list2 = [1, 7, 6, 3, 7, 9, 10, 11]               # printing original listsprint("The original list 1 is : " + str(test_list1))print("The original list 2 is : " + str(test_list2))  # getting duplicate elements list using list comprehensiontemp = [idx for idx, val in enumerate(test_list1) if val in test_list1[:idx]]  # excluding duplicate indices from other listres = [ele for idx, ele in enumerate(test_list2) if idx not in temp]  # printing resultprint("The list after removing duplicate indices : " + str(res))
Output:
The original list 1 is : [3, 5, 6, 5, 3, 7, 8, 6]
The original list 2 is : [1, 7, 6, 3, 7, 9, 10, 11]
The list after removing duplicate indices : [1, 7, 6, 9, 10]
varshagumber28
Python list-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary
How to print without newline in Python?