Python – Difference of List keeping duplicates
The problem of finding difference between list, i.e removing elements that occur in one list and not in other is discussed before. But the usage of sets ignores duplicates and we sometimes, require to remove the exact elements that occur in lists. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop This is brute way in which this task can be performed. In this, we extract the elements in a form in which we remove elements each time and break the loop to remove one element at a time.
Python3
test_list1 = [ 4 , 5 , 7 , 4 , 3 ]
test_list2 = [ 7 , 3 , 4 ]
print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
for ele in test_list2:
for sub in test_list1:
if ele = = sub:
test_list1.remove(sub)
break
print (" List after performing difference : " + str (test_list1))
|
Output :
The original list 1 is : [4, 5, 7, 4, 3]
The original list 2 is : [7, 3, 4]
List after performing difference : [5, 4]
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 pop() + list comprehension + index() This task can also be performed using combination of above functionalities. In this, we just iterate the list using list comprehension and remove element using index() and pop().
Python3
test_list1 = [ 4 , 5 , 7 , 4 , 3 ]
test_list2 = [ 7 , 3 , 4 ]
print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
[test_list1.pop(test_list1.index(idx)) for idx in test_list2]
print (" List after performing difference : " + str (test_list1))
|
Output :
The original list 1 is : [4, 5, 7, 4, 3]
The original list 2 is : [7, 3, 4]
List after performing difference : [5, 4]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(k), where k is the number of unique elements in the input list (determined by creating a set of the input list).
Method #3 : Using collections.Counter()
This task can also be performed using collections.Counter() which simply counts the occurrence of each element and subtracts the corresponding elements.
Python3
test_list1 = [ 4 , 5 , 7 , 4 , 3 ]
test_list2 = [ 7 , 3 , 4 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
from collections import Counter
res = Counter(test_list1) - Counter(test_list2)
print ( "List after performing difference : " + str ( list (res.elements())))
|
Output
The original list 1 is : [4, 5, 7, 4, 3]
The original list 2 is : [7, 3, 4]
List after performing difference : [4, 5]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using the deque() function from the collections module:
Algorithm:
- Convert test_list1 and test_list2 to deques.
- For each element x in test_list2, try to remove it from the deque d. Ignore ValueError exceptions if x is not in d.
- Convert d back to a list and store it in res.
Python3
from collections import deque
test_list1 = [ 4 , 5 , 7 , 4 , 3 ]
test_list2 = [ 7 , 3 , 4 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
d = deque(test_list1)
d2 = deque(test_list2)
for x in d2:
try :
d.remove(x)
except ValueError:
pass
res = list (d)
print ( "List after performing difference : " + str (res))
|
Output
The original list 1 is : [4, 5, 7, 4, 3]
The original list 2 is : [7, 3, 4]
List after performing difference : [5, 4]
Time Complexity:
- Converting test_list1 and test_list2 to deques takes O(n) time where n is the length of test_list1.
- The for loop iterates over each element in test_list2, so its time complexity is O(m) where m is the length of test_list2.
- The remove() method on a deque has O(1) time complexity on average, but O(n) in the worst case when the element is not found, so the worst-case time complexity of the try-except block is O(mn).
- Converting the deque back to a list takes O(n) time.
- Overall, the worst-case time complexity of the algorithm is O(mn).
Auxiliary Space:
- Converting test_list1 and test_list2 to deques uses O(n) space.
- The res list uses O(n) space.
- The d and d2 deques use O(n) + O(m) = O(n+m) space.
- Overall, the space complexity of the algorithm is O(n+m).
Method #5 : Using itertools and collections:
Algorithm :
- Import the required modules, itertools and collections.
- Initialize two lists, test_list1 and test_list2 with some elements.
- Print the original lists.
- Use itertools.chain() function to concatenate the elements of both lists into a single iterator.
- Use collections.Counter() function on the iterator to get the count of each element.
- Use the ‘-‘ operator between the two Counter objects to find the difference between the two lists, keeping duplicates.
- Convert the result Counter object to a list using the elements() function.
- Print the final result.
Python3
import itertools
from collections import Counter
test_list1 = [ 4 , 5 , 7 , 4 , 3 ]
test_list2 = [ 7 , 3 , 4 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = Counter(itertools.chain(test_list1)) - Counter(itertools.chain(test_list2))
print ( "List after performing difference : " + str ( list (res.elements())))
|
Output
The original list 1 is : [4, 5, 7, 4, 3]
The original list 2 is : [7, 3, 4]
List after performing difference : [4, 5]
Time complexity: O(n), where n is the length of the concatenated list of elements. This is because we need to iterate over all the elements of the concatenated list to count their occurrences.
Auxiliary Space: O(n), where n is the length of the concatenated list of elements. This is because we need to create two Counter objects, each of which would store the count of each element in the concatenated list.
Last Updated :
31 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...