itsjay
itsjay

Reputation: 1

Removing duplicates between the two lists while maintaining the duplicates in one specific list

I am currently creating my program regarding data structures using Python 2.7.13 and I find it hard to finish it. With this, i want to remove all the duplicates between the two lists while maintaining the duplicate elements in one specific list considering that it is not the same with the other list.

To make it clear, I will be presenting an example, Suppose:

input:

    a= [1,2,2,5,6,6]
    b= [2,5,7,9]

expected output:

    c= [7,9,1,6,6]

Upvotes: 0

Views: 349

Answers (1)

ssm
ssm

Reputation: 5373

You can start with

>>> set(a).intersection(set(b))
 {2, 5}

>>> set(a).union(set(b)) - set(a).intersection(set(b))  
 {1, 6, 7, 9}

Let us say you have a set called common

common = list(set(a).union(set(b)) - set(a).intersection(set(b)))

Then you can find your list as:

>>> [c for c in (a+b)  if c in common]
[1, 6, 6, 7, 9]

Upvotes: 2

Related Questions