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
test_list1 = [ 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 , 6 , 6 ]
test_list2 = [ 8 , 3 , 7 , 5 , 4 , 1 , 0 , 9 , 4 , 2 ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
temp = dict ()
a = []
for idx in range ( len (test_list1)):
if test_list1[idx] not in a:
a.append(test_list1[idx])
temp[test_list1[idx]] = test_list2[idx]
res2 = list (temp.values())
res1 = a
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
test_list1 = [ 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 , 6 , 6 ]
test_list2 = [ 8 , 3 , 7 , 5 , 4 , 1 , 0 , 9 , 4 , 2 ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res1 = []
res2 = []
for a, b in zip (test_list1, test_list2):
if a not in res1:
res1.append(a)
res2.append(b)
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
import operator as op
test_list1 = [ 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 , 6 , 6 ]
test_list2 = [ 8 , 3 , 7 , 5 , 4 , 1 , 0 , 9 , 4 , 2 ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
temp = dict ()
a = []
for idx in range ( len (test_list1)):
if op.countOf(a, test_list1[idx]) = = 0 :
a.append(test_list1[idx])
temp[test_list1[idx]] = test_list2[idx]
res2 = list (temp.values())
res1 = a
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:
- Create an empty list res1 to hold unique elements of test_list1.
- Create an empty list res2 to hold corresponding elements of test_list2 for unique elements in res1.
- 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.
- Convert the keys of the dictionary to a list and store it in res1.
- 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.
- Print the results.
Python3
from collections import OrderedDict
test_list1 = [ 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 , 6 , 6 ]
test_list2 = [ 8 , 3 , 7 , 5 , 4 , 1 , 0 , 9 , 4 , 2 ]
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]
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:
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:
- Initialize the two lists, test_list1 and test_list2.
- Print the original lists.
- Get the unique elements from test_list1 using set() and convert it to list and store it in res1.
- Get the corresponding values for unique elements from test_list2 using list comprehension and store it in res2.
- Print the final result res1 and res2
Python3
test_list1 = [ 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 , 6 , 6 ]
test_list2 = [ 8 , 3 , 7 , 5 , 4 , 1 , 0 , 9 , 4 , 2 ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res1 = list ( set (test_list1))
res2 = [test_list2[test_list1.index(i)] for i in res1]
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:
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
test_list1 = [ 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 , 6 , 6 ]
test_list2 = [ 8 , 3 , 7 , 5 , 4 , 1 , 0 , 9 , 4 , 2 ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res1 = np.unique(test_list1)
res2 = [test_list2[np.where(test_list1 = = i)[ 0 ][ 0 ]] for i in res1]
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]
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.
Similar Reads
Python - Remove Columns of Duplicate Elements
Given a Matrix, write a Python program to remove whole column if duplicate occurs in any row. Examples: Input : test_list = [[4, 3, 5, 2, 3], [6, 4, 2, 1, 1], [4, 3, 9, 3, 9], [5, 4, 3, 2, 1]] Output : [[4, 3, 5], [6, 4, 2], [4, 3, 9], [5, 4, 3]] Explanation : 1 has duplicate as next element hence 5
3 min read
Python | Remove duplicates based on Kth element tuple list
Sometimes, while working with records, we may have a problem in which we need to remove duplicates based on Kth element of a tuple in the list. This problem has application in domains that uses records as input. Let's discuss certain ways in which this problem can be solved. Method #1: Using loop Th
8 min read
Remove Unordered Duplicate Elements from a List - Python
Given a list of elements, the task is to remove all duplicate elements from the list while maintaining the original order of the elements.For example, if the input is [1, 2, 2, 3, 1] the expected output is [1, 2, 3]. Let's explore various methods to achieve this in Python. Using setWe can initialize
3 min read
Python | Duplicate substring removal from list
Sometimes we can come to the problem in which we need to deal with certain strings in a list that are separated by some separator and we need to remove the duplicates in each of these kinds of strings. Simple shorthands to solve this kind of problem is always good to have. Let's discuss certain ways
7 min read
Removing Duplicates of a List of Sets in Python
Dealing with sets in Python allows for efficient handling of unique elements, but when working with a list of sets, you may encounter scenarios where duplicates need to be removed. In this article, we will see how we can remove duplicates of a list of sets in Python. Remove Duplicates of a List of S
2 min read
Remove Duplicate Strings from a List in Python
Removing duplicates helps in reducing redundancy and improving data consistency. In this article, we will explore various ways to do this. set() method converts the list into a set, which automatically removes duplicates because sets do not allow duplicate values. [GFGTABS] Python a = ["Learn
3 min read
Python - Remove Duplicates from a List
Removing duplicates from a list is a common operation in Python which is useful in scenarios where unique elements are required. Python provides multiple methods to achieve this. Using set() method is most efficient for unordered lists. Converting the list to a set removes all duplicates since sets
2 min read
Python program to remove duplicate elements index from other list
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 1
7 min read
Python | Remove duplicate tuples from list of tuples
Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples: Input : [(1, 2), (5, 7), (3, 6), (1, 2)] Output : [(1, 2), (5, 7), (3, 6)] Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')] Output : [('a', 'z'), ('a', 'x'), ('z', 'x
5 min read
Python | Remove duplicates from nested list
The task of removing duplicates many times in the recent past, but sometimes when we deal with the complex data structure, in those cases we need different techniques to handle this type of problem. Let's discuss certain ways in which this task can be achieved. Method #1 : Using sorted() + set() Thi
5 min read