Open In App
Related Articles

Python – Difference of List keeping duplicates

Improve Article
Improve
Save Article
Save
Like Article
Like

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




# Python3 code to demonstrate
# Difference of List keeping duplicates
# using loop
 
# Initializing lists
test_list1 = [4, 5, 7, 4, 3]
test_list2 = [7, 3, 4]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Difference of List keeping duplicates
# using loop
for ele in test_list2:
    for sub in test_list1:
        if ele == sub:
            test_list1.remove(sub)
            break
 
# printing result
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




# Python3 code to demonstrate
# Difference of List keeping duplicates
# using pop() + list comprehension + index()
 
# Initializing lists
test_list1 = [4, 5, 7, 4, 3]
test_list2 = [7, 3, 4]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Difference of List keeping duplicates
# using pop() + list comprehension + index()
[test_list1.pop(test_list1.index(idx)) for idx in test_list2]
 
# printing result
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




# Python3 code to demonstrate
# Difference of List keeping duplicates
# using collections.Counter()
   
# Initializing lists
test_list1 = [4, 5, 7, 4, 3]
test_list2 = [7, 3, 4]
   
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
   
# Difference of List keeping duplicates
# using collections.Counter()
from collections import Counter
res = Counter(test_list1) - Counter(test_list2)
   
# printing result
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:

  1. Convert test_list1 and test_list2 to deques.
  2. For each element x in test_list2, try to remove it from the deque d. Ignore ValueError exceptions if x is not in d.
  3. Convert d back to a list and store it in res.
     

Python3




from collections import deque
# Initializing lists
test_list1 = [4, 5, 7, 4, 3]
test_list2 = [7, 3, 4]
# printing original lists
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)
# printing result
print ("List after performing difference : " + str(res))
#This code is contributed by Jyothi pinjala.

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 :

  1. Import the required modules, itertools and collections.
  2. Initialize two lists, test_list1 and test_list2 with some elements.
  3. Print the original lists.
  4. Use itertools.chain() function to concatenate the elements of both lists into a single iterator.
  5. Use collections.Counter() function on the iterator to get the count of each element.
  6. Use the ‘-‘ operator between the two Counter objects to find the difference between the two lists, keeping duplicates.
  7. Convert the result Counter object to a list using the elements() function.
  8. Print the final result.

Python3




# Python3 code to demonstrate
# Difference of List keeping duplicates
# using collections.Counter() and itertools.chain()
 
import itertools
from collections import Counter
 
# Initializing lists
test_list1 = [4, 5, 7, 4, 3]
test_list2 = [7, 3, 4]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Difference of List keeping duplicates
# using collections.Counter() and itertools.chain()
res = Counter(itertools.chain(test_list1)) - Counter(itertools.chain(test_list2))
 
# printing result
print ("List after performing difference : " + str(list(res.elements())))
 
#This code is contributed by Rayudu.

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
Similar Reads
Related Tutorials