Skip to content
Related Articles
Open in App
Not now

Related Articles

Python program to remove duplicate elements index from other list

Improve Article
Save Article
  • Last Updated : 10 Mar, 2023
Improve Article
Save Article

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 lists
test_list1 = [3, 5, 6, 5, 3, 7, 8, 6]
test_list2 = [1, 7, 6, 3, 7, 9, 10, 11]
              
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
oc_set = set()
temp = []
 
# getting duplicate elements list
for 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 list
res = [ele for idx, ele in enumerate(test_list2) if idx not in temp]
 
# printing result
print("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]

Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

 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 lists
test_list1 = [3, 5, 6, 5, 3, 7, 8, 6]
test_list2 = [1, 7, 6, 3, 7, 9, 10, 11]
              
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# getting duplicate elements list using list comprehension
temp = [idx for idx, val in enumerate(test_list1) if val in test_list1[:idx]]
 
# excluding duplicate indices from other list
res = [ele for idx, ele in enumerate(test_list2) if idx not in temp]
 
# printing result
print("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 3: Using a dictionary to store the index of each element in the first list

Step-by-step algorithm:

  1. Initialize two lists, test_list1 and test_list2.
  2. Create a dictionary index_dict to store the index of each element in test_list1.
  3. Initialize a list res to store the filtered elements from test_list2.
  4. Loop through each element x and its index i in test_list2.
  5. Check if the index i is not present in the list of indexes of duplicates in test_list1.
  6. If i is not present, then add the element x to the list res.
  7. Print the list res.

Python3




test_list1 = [3, 5, 6, 5, 3, 7, 8, 6]
test_list2 = [1, 7, 6, 3, 7, 9, 10, 11]
 
# create a dictionary to store the index of each element in test_list1
index_dict = {val: i for i, val in enumerate(test_list1)}
 
# remove elements from test_list2 that have the same index as a duplicate in test_list1
res = [x for i, x in enumerate(test_list2) if i not in [index_dict[val] for val in test_list1 if test_list1.count(val) > 1]]
print(res)

Output

[1, 7, 6, 9, 10]

Time complexity:

The time complexity of the algorithm is O(n^2), where n is the length of test_list1. This is because the list comprehension [index_dict[val] for val in test_list1 if test_list1.count(val) > 1] has a time complexity of O(n^2) due to the count() function, and this comprehension is executed for each element in test_list2.
Auxiliary space:

The auxiliary space complexity of the algorithm is O(n), where n is the length of test_list1. This is because the dictionary index_dict has a space complexity of O(n), and the list res can have a maximum of n elements.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!