In Python you can use a.intersection(b)
to find the items common to both sets.
Is there a way to do the disjoint opposite version of this? Items that are not common to both a
and b
; the unique items in a
unioned with the unique items in b
?
The best way is a list comprehension.
You can join both lists
Try this code for (set(a) - intersection(a&b))
the output is
[1,4,5,6]
I hope, it will workYou are looking for the symmetric difference; all elements that appear only in set a or in set b, but not both:
From the
set.symmetric_difference()
method documentation:You can use the
^
operator too, if botha
andb
are sets:while
set.symmetric_difference()
takes any iterable for the other argument.The output is the equivalent of
(a | b) - (a & b)
, the union of both sets minus the intersection of both sets.e, f are two list you want to check disjoint
This loops around to list and returns the disjoint list