Python Set difference_update() Method Tutorial

In this section we will learn what the Set difference_update() method is and how to use it in Python.

What is Set difference_update() Method in Python?

The Python Set difference_update() method is used to remove those elements from the Set object (the one that invoked the method) that also exist in a reference Set that we put as the argument of this method.

Note: there’s another method called difference() that does the same task. The difference between the two methods is that the difference() method returns a new Set object as a result (So the original Set won’t change) but the difference_update() will change the original Set object (the one that invoked this method).

Python Set difference_update() Method Syntax:

set.difference_update(referenceSetObject)

Set difference_update() Method Parameter:

The method takes one argument and that is a reference to a Set object.

Set difference_update() method Return Value

The method does not return a value.

Example: using python set difference_update() method

set1 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}

set2 = {1,2,3,4,5}

result = set1.difference_update(set2)

print(result)

print(set1)

Output:

None

{6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

Leave a Reply