Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Cross List Sync on duplicate elements removal

  • Last Updated : 19 May, 2021

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. 
 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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]

 

 
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]

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!