Open In App

Python – Cross List Sync on duplicate elements removal

Last Updated : 15 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given two lists, perform syncing of all the elements of list with other list when removal of duplicate elements in first list. 
 

Input : test_list1 = [1, 1, 2, 2, 3, 3], test_list2 = [8, 3, 7, 5, 4, 1] 
Output : [1, 2, 3], [8, 7, 4] 
Explanation : After removal of duplicates (1, 2, 3), corresponding elements are removed from 2nd list, and hence (8, 7, 4) are mapped.
Input : test_list1 = [1, 2, 2, 2, 2], test_list2 = [8, 3, 7, 5, 4, 1] 
Output : [1, 2], [8, 3] 
Explanation : Elements removed are (2, 2, 2), [1, 2] are mapped to [8, 3] as expected. 
 

Method #1 : Using loop + dict() 
The above functions provide brute force way to solve this problem. In this, we memorize the elements with its counterpart using dictionary and loop conditionals. 
 

Python3




# Python3 code to demonstrate working of
# Cross List Sync on duplicate elements removal
# Using loop + dict()
 
# initializing lists
test_list1 = [2, 2, 3, 4, 4, 4, 5, 5, 6, 6]
test_list2 = [8, 3, 7, 5, 4, 1, 0, 9, 4, 2]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Cross List Sync on duplicate elements removal
temp = dict()
a = []
for idx in range(len(test_list1)):
    if test_list1[idx] not in a:
        a.append(test_list1[idx])
         
        # performing memoize using dictionary
        temp[test_list1[idx]] = test_list2[idx]
 
res2 = list(temp.values())
res1 = a
 
# printing result
print("List 1 : " + str(res1))
print("Sync List : " + str(res2))


Output : 

The original list 1 : [2, 2, 3, 4, 4, 4, 5, 5, 6, 6]
The original list 2 : [8, 3, 7, 5, 4, 1, 0, 9, 4, 2]
List 1 : [2, 3, 4, 5, 6]
Sync List : [8, 7, 5, 0, 4]

 

Time complexity: O(n), where n is the length of the input lists.
Auxiliary Space: O(n), where n is the length of the input lists, due to the use of a dictionary to store memoized values.

Method #2 : Using loop + zip() 
This is yet another way in which this task can be performed. In this, we sync both the lists using zip(). The similar memorize logic is implemented as above task and corresponding valid other list element is appended to result.

Python3




# Python3 code to demonstrate working of
# Cross List Sync on duplicate elements removal
# Using loop + zip()
 
# initializing lists
test_list1 = [2, 2, 3, 4, 4, 4, 5, 5, 6, 6]
test_list2 = [8, 3, 7, 5, 4, 1, 0, 9, 4, 2]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Cross List Sync on duplicate elements removal
res1 = []
res2 = []
 
# both lists are combined index wise using zip()
for a, b in zip(test_list1, test_list2):
    if a not in res1:
        res1.append(a)
        res2.append(b)
         
# printing result
print("List 1 : " + str(res1))
print("Sync List : " + str(res2))


Output : 

The original list 1 : [2, 2, 3, 4, 4, 4, 5, 5, 6, 6]
The original list 2 : [8, 3, 7, 5, 4, 1, 0, 9, 4, 2]
List 1 : [2, 3, 4, 5, 6]
Sync List : [8, 7, 5, 0, 4]

 

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

Method #3:Using operator.countOf() method

Python3




# Python3 code to demonstrate working of
# Cross List Sync on duplicate elements removal
import operator as op
# initializing lists
test_list1 = [2, 2, 3, 4, 4, 4, 5, 5, 6, 6]
test_list2 = [8, 3, 7, 5, 4, 1, 0, 9, 4, 2]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Cross List Sync on duplicate elements removal
temp = dict()
a = []
for idx in range(len(test_list1)):
    if op.countOf(a, test_list1[idx]) == 0:
        a.append(test_list1[idx])
 
        # performing memoize using dictionary
        temp[test_list1[idx]] = test_list2[idx]
 
res2 = list(temp.values())
res1 = a
 
# printing result
print("List 1 : " + str(res1))
print("Sync List : " + str(res2))


Output

The original list 1 : [2, 2, 3, 4, 4, 4, 5, 5, 6, 6]
The original list 2 : [8, 3, 7, 5, 4, 1, 0, 9, 4, 2]
List 1 : [2, 3, 4, 5, 6]
Sync List : [8, 7, 5, 0, 4]

Time Complexity: O(N)
Auxiliary Space: O(N)

Method #4: Using collections.OrderedDict() function.

Algorithm:

  1. Create an empty list res1 to hold unique elements of test_list1.
  2. Create an empty list res2 to hold corresponding elements of test_list2 for unique elements in res1.
  3. Use the OrderedDict class from the collections module to create a dictionary with the elements of test_list1 as keys and None as values. Since OrderedDict maintains the order of keys as they were first inserted, this will automatically remove duplicates from test_list1.
  4. Convert the keys of the dictionary to a list and store it in res1.
  5. Iterate through the elements in res1 and use the index method of test_list1 to find the corresponding element in test_list2. Append this element to res2.
  6. Print the results.
     

Python3




# Python3 code to demonstrate working of
# Cross List Sync on duplicate elements removal
from collections import OrderedDict
# initializing lists
test_list1 = [2, 2, 3, 4, 4, 4, 5, 5, 6, 6]
test_list2 = [8, 3, 7, 5, 4, 1, 0, 9, 4, 2]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
res1 = list(OrderedDict.fromkeys(test_list1))
res2 = [test_list2[test_list1.index(i)] for i in res1]
 
 
# printing result
print("List 1 : " + str(res1))
print("Sync List : " + str(res2))
#this code contributed by tvsk


Output

The original list 1 : [2, 2, 3, 4, 4, 4, 5, 5, 6, 6]
The original list 2 : [8, 3, 7, 5, 4, 1, 0, 9, 4, 2]
List 1 : [2, 3, 4, 5, 6]
Sync List : [8, 7, 5, 0, 4]

Time complexity:

Creating the OrderedDict has a time complexity of O(n), where n is the length of the input list.
The loop for finding the corresponding elements and appending them to res2 has a time complexity of O(n^2), since the index method is called within a loop, which has a worst-case time complexity of O(n). Therefore, the overall time complexity of the algorithm is O(n^2).
Auxiliary space:

The algorithm uses two additional lists (res1 and res2) to store the results, as well as an OrderedDict to store the unique elements of test_list1. Therefore, the overall auxiliary space complexity of the algorithm is O(n).

Method#5:Using set() and list comprehension

Algorithm:

  1. Initialize the two lists, test_list1 and test_list2.
  2. Print the original lists.
  3. Get the unique elements from test_list1 using set() and convert it to list and store it in res1.
  4. Get the corresponding values for unique elements from test_list2 using list comprehension and store it in res2.
  5. Print the final result res1 and res2

Python3




# initializing lists
test_list1 = [2, 2, 3, 4, 4, 4, 5, 5, 6, 6]
test_list2 = [8, 3, 7, 5, 4, 1, 0, 9, 4, 2]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Cross List Sync on duplicate elements removal
res1 = list(set(test_list1))
 
# List comprehension to get corresponding values
res2 = [test_list2[test_list1.index(i)] for i in res1]
 
# printing result
print("List 1 : " + str(res1))
print("Sync List : " + str(res2))
#This code is contributed by Vinay Pinjala.


Output

The original list 1 : [2, 2, 3, 4, 4, 4, 5, 5, 6, 6]
The original list 2 : [8, 3, 7, 5, 4, 1, 0, 9, 4, 2]
List 1 : [2, 3, 4, 5, 6]
Sync List : [8, 7, 5, 0, 4]

Time Complexity:
The time complexity of this code is O(n^2) because index() method used to find the index of an element in a list has time complexity O(n) and it is used inside a loop that runs n times. Also, set() has time complexity of O(n).

Auxiliary Space:
The space complexity of this code is O(n) because we are creating two lists res1 and res2, both of size n. Also, set() creates a set of unique elements from the given list, which can take extra space.

Method#6: Using numpy:

Algorithm:

1.Initialize two lists, test_list1 and test_list2, with the input values.
2.Remove duplicates from test_list1 using any appropriate method such as set, numpy, or itertools.
3.Create an empty list, res2, to hold the corresponding values from test_list2.
4.Loop through the elements in the deduplicated test_list1.
5.For each element, find its index in the original test_list1 using the index() method.
6.Use this index to get the corresponding element from test_list2 and append it to res2.
7.Print the deduplicated test_list1 and res2 to check the synchronization.

Python3




import numpy as np
 
# initializing lists
test_list1 = [2, 2, 3, 4, 4, 4, 5, 5, 6, 6]
test_list2 = [8, 3, 7, 5, 4, 1, 0, 9, 4, 2]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# remove duplicates from test_list1 using numpy
res1 = np.unique(test_list1)
 
# get corresponding values from test_list2
res2 = [test_list2[np.where(test_list1 == i)[0][0]] for i in res1]
 
# print results
print("List 1 : " + str(res1))
print("Sync List : " + str(res2))
#This code is contributed by Jyothi pinjala


Output:

The original list 1 : [2, 2, 3, 4, 4, 4, 5, 5, 6, 6]
The original list 2 : [8, 3, 7, 5, 4, 1, 0, 9, 4, 2]
List 1 : [2 3 4 5 6]
Sync List : [8, 7, 5, 0, 4]

The time complexity : O(n log n) ,due to the use of the numpy.unique() function, which has a time complexity of O(n log n), where n is the length of the input list.

The space complexity : O(n), where n is the length of the input list. This is because the numpy.unique() function creates a new array of unique elements, which can potentially be as large as the original input array. The resulting list res2 also takes up space proportional to the length of the input list.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads