Python prorgam to remove duplicate elements index from other list

When working with two lists, you may need to remove elements from the second list at positions where duplicate values occur in the first list. This can be accomplished using enumerate(), sets for tracking duplicates, and list comprehension.

Problem Understanding

Given two lists of the same length, we want to:

  • Find duplicate elements in the first list
  • Get the indices where these duplicates occur
  • Remove elements from the second list at those duplicate indices

Example

Here's how to remove elements from the second list based on duplicate indices from the first list −

my_list_1 = [4, 5, 6, 5, 4, 7, 8, 6]
my_list_2 = [1, 7, 6, 4, 7, 9, 10, 11]

print("The first list is :")
print(my_list_1)
print("The second list is :")
print(my_list_2)

temp_set = set()
temp = []

for index, value in enumerate(my_list_1):
    if value not in temp_set:
        temp_set.add(value)
    else:
        temp.append(index)

my_result = [element for index, element in enumerate(my_list_2) if index not in temp]

print("The result is :")
print(my_result)
The first list is :
[4, 5, 6, 5, 4, 7, 8, 6]
The second list is :
[1, 7, 6, 4, 7, 9, 10, 11]
The result is :
[1, 7, 6, 9, 10]

How It Works

The algorithm works in three steps:

  1. Track unique values: Use a set to keep track of values we've already seen in the first list
  2. Find duplicate indices: When we encounter a value that's already in our set, we record its index as a duplicate
  3. Filter second list: Use list comprehension to keep only elements from the second list whose indices are not in our duplicate indices list

Step-by-Step Breakdown

Let's trace through the example:

  • Index 0: value 4 (first occurrence) − keep
  • Index 1: value 5 (first occurrence) − keep
  • Index 2: value 6 (first occurrence) − keep
  • Index 3: value 5 (duplicate) − mark index 3 for removal
  • Index 4: value 4 (duplicate) − mark index 4 for removal
  • Index 5: value 7 (first occurrence) − keep
  • Index 6: value 8 (first occurrence) − keep
  • Index 7: value 6 (duplicate) − mark index 7 for removal

Therefore, indices 3, 4, and 7 are removed from the second list, leaving elements at indices 0, 1, 2, 5, 6.

Alternative Approach

You can also solve this using a more concise approach with dictionary tracking −

my_list_1 = [4, 5, 6, 5, 4, 7, 8, 6]
my_list_2 = [1, 7, 6, 4, 7, 9, 10, 11]

seen = {}
duplicate_indices = []

for i, val in enumerate(my_list_1):
    if val in seen:
        duplicate_indices.append(i)
    else:
        seen[val] = i

result = [my_list_2[i] for i in range(len(my_list_2)) if i not in duplicate_indices]
print("Result:", result)
Result: [1, 7, 6, 9, 10]

Conclusion

This technique effectively removes elements from one list based on duplicate positions in another list. Use enumerate() with sets to track duplicates efficiently, then apply list comprehension for filtering.

Updated on: 2026-03-26T02:19:55+05:30

315 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements