Python subtract one set from another

What distinguishes - and .difference() on sets? Obviously the syntax is not the same. One is a binary operator, and the other is an instance method. What else?

s1 = set([1,2,3])
s2 = set([3,4,5])

>>> s1 - s2
set([1, 2])
>>> s1.difference(s2)
set([1, 2])

Python subtract one set from another

asked Jun 22, 2015 at 18:00

David542David542

104k158 gold badges443 silver badges751 bronze badges

3

set.difference, set.union... can take any iterable as the second arg while both need to be sets to use -, there is no difference in the output.

Operation         Equivalent   Result
s.difference(t)   s - t        new set with elements in s but not in t

With .difference you can do things like:

s1 = set([1,2,3])

print(s1.difference(*[[3],[4],[5]]))

{1, 2}

It is also more efficient when creating sets using the *(iterable,iterable) syntax as you don't create intermediary sets, you can see some comparisons here

Nam G VU

31.6k67 gold badges221 silver badges356 bronze badges

answered Jun 22, 2015 at 18:02

Python subtract one set from another

1

At a glance, it may not be quite evident from the documentation, but buried deep inside a paragraph it is dedicated to differentiate the method call with the operator version:

Note, the non-operator versions of union(), intersection(), difference(), and symmetric_difference(), issubset(), and issuperset() methods will accept any iterable as an argument. In contrast, their operator based counterparts require their arguments to be sets. This precludes error-prone constructions like set('abc') & 'cbs' in favor of the more readable set('abc').intersection('cbs').

Python subtract one set from another

answered Jun 22, 2015 at 18:10

The documentation appears to suggest that difference can take multiple sets, so it is possible that it might be more efficient and clearer for things like:

s1 = set([1, 2, 3, 4])
s2 = set([2, 5])
s3 = set([3, 6])
s1.difference(s2, s3) # instead of s1 - s2 - s3

but I would suggest some testing to verify.

answered Jun 22, 2015 at 18:08

Python subtract one set from another

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The difference between the two sets in Python is equal to the difference between the number of elements in two sets. The function difference() returns a set that is the difference between two sets. Let’s try to find out what will be the difference between two sets A and B. Then (set A – set B) will be the elements present in set A but not in B and (set B – set A) will be the elements present in set B but not in set A.
    Example:

    set A = {10, 20, 30, 40, 80}
    set B = {100, 30, 80, 40, 60}
    
    set A - set B = {10, 20}
    set B - set A = {100, 60}
    
    Explanation: A - B is equal to the elements present in A but not in B
                 B - A is equal to the elements present in B but not in A
    

    Let’s look at the Venn diagram of the following difference set function.

    Python subtract one set from another

    Syntax:

    set_A.difference(set_B) for (A - B)
    set _B.difference(set_A) for (B - A)
    

    In this program, we will try to find out the difference between two sets set_A and set_B, both the way:

    A = {10, 20, 30, 40, 80}

    B = {100, 30, 80, 40, 60}

    print (A.difference(B))

    print (B.difference(A))

    Output:

    {10, 20}
    {100, 60}
    

    We can also use – operator to find the difference between two sets.

    A = {10, 20, 30, 40, 80}

    B = {100, 30, 80, 40, 60}

    print (A - B)

    print (B - A)

    Output:

    {10, 20}
    {100, 60}
    

    If we have equal sets then it will return the null set.

    A = {10, 20, 30, 40, 80}

    B = {10, 20, 30, 40, 80, 100}

    print (A - B)


    How do you subtract a set from another in Python?

    Python Set | difference() The difference between the two sets in Python is equal to the difference between the number of elements in two sets. The function difference() returns a set that is the difference between two sets.

    How do you find the difference between two sets?

    The difference of two sets, written A - B is the set of all elements of A that are not elements of B..
    A - A =∅.
    A - ∅ = A..
    ∅ - A = ∅.
    A - U = ∅.
    (AC)C = A..
    DeMorgan's Law I: (A ∩ B)C = AC ∪ B. C.
    DeMorgan's Law II: (A ∪ B)C = AC ∩ B. C.

    Can you subtract sets?

    Mathwords: Set Subtraction. A way of modifying a set by removing the elements belonging to another set. Subtraction of sets is indicated by either of the symbols – or \. For example, A minus B can be written either A – B or A \ B.

    How do you compare two set values in Python?

    The set() function and == operator.
    list1 = [11, 12, 13, 14, 15].
    list2 = [12, 13, 11, 15, 14].
    a = set(list1).
    b = set(list2).
    if a == b:.
    print("The list1 and list2 are equal").
    print("The list1 and list2 are not equal").