Skip to content
Related Articles

Related Articles

Python – Cross List Sync on duplicate elements removal
  • Last Updated : 28 Jul, 2020
GeeksforGeeks - Summer Carnival Banner

Given two lists, perform syncing of all the elements of list with other list when removal of duplcate 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 memoize the elements with its counterpart using dictionary and loop conditionals.




# 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 memoize logic is implemented as above task and corresponding valid other list element is appended to result.




# 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]

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :