Skip to main content

Python Set difference_update() Function

The Set difference_update() method finds the difference between two sets. It modifies the given set that contains all the elements of the first set that are not present in the second set.

Syntax​

my_set.difference_update(set1, set2, ...)

difference_update() Parameters​

Python Set difference_update() function parameters:

ParameterConditionDescription
set1, set2, ...OptionalA comma-separated list of one or more sets to find differences in
note

You can also pass iterables like list, tuple, dictionary or string. In that case, difference_update() method first converts the iterables into sets and then computes the difference.

difference_update() Return Value​

Python Set difference_update() function does not return any value: it modifies the original set in-place.

Examples​

Example 1: Difference between Two Sets​

A = {'Tom', 'Anna', 'David'}
B = {'David', 'Tom'}

A.difference_update(B)

print(A) # Output: {'Anna'}

output

{'Anna'}

Example 2: Difference between Multiple Sets​

The difference_update() method allows to compute the difference between multiple sets:

A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
B = {3, 4, 5, 6, 7}
C = {6, 7, 8, 9, 10}

A.difference_update(B, C)

print(A) # Output: {1, 2}

output

{1, 2}

Example 3: Difference between a Set and a List​

You can also pass iterables like list: difference_update() first converts the iterables to sets and then computes the difference in-place.

For example:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create a List B
B = ['d', 'e', 'f']

A.difference_update(B)

print(A) # Output: {'i', 'a', 'o', 'u'}

output

{'i', 'a', 'o', 'u'}

Example 4: Difference between a Set and a Tuple​

You can also pass iterables like tuple: difference_update() first converts the iterables to sets and then computes the difference in-place.

For example:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create a Tuple B
B = ('d', 'e', 'f')

A.difference_update(B)

print(A) # Output: {'i', 'a', 'o', 'u'}

output

{'i', 'a', 'o', 'u'}

Example 5: Difference between a Set and a Dictionary​

You can also pass iterables like dictionary: difference_update() first converts the iterables to sets and then computes the difference in-place.

note

The dictionary keys are used for difference_update(), not the values!

For example:

# create a Set A1 and A2
A1 = {'a', 'e', 'i', 'o', 'u'}
A2 = {'a', 'e', 'i', 'o', 'u'}

# create Dictionary B and C
B = {1: 'd', 2: 'e', 3: 'f'}
C = {'d': 1, 'e': 2, 'f': 3}

A1.difference_update(B)
A2.difference_update(C)

print(A1) # Output: {'o', 'e', 'i', 'a', 'u'}
print(A2) # Output: {'o', 'a', 'u', 'i'}

output

{'o', 'e', 'i', 'a', 'u'}
{'o', 'a', 'u', 'i'}

Example 6: Difference between a Set and a String​

You can also pass iterables like string: difference_update() first converts the iterables to sets and then computes the difference in-place.

For example:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create a String B
B = 'def'

A.difference_update(B)

print(A) # Output: {'i', 'a', 'o', 'u'}

output

{'i', 'a', 'o', 'u'}

Equivalent Operator –= for difference_update()​

Set difference can be performed with the -= operator as well, providing an alternative to the difference_update() method.

For example:

A = {1, 2, 3, 4, 5}
B = {3, 4, 5, 6, 7}

# equivalent to: A.difference_update(B)
A -= B

print(A) # Output: {1, 2}

output

{1, 2}

Of course, you can use the -= operator with multiple sets:

A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
B = {3, 4, 5, 6, 7}
C = {6, 7, 8, 9, 10}

# equivalent to: A.difference_update(B, C)
A -= B | C

print(A) # Output: {1, 2}

output

{1, 2}