English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced Python knowledge

Python reference manual

Python set difference_update() usage and examples

Méthodes de collection en Python

Difference_update() uses the difference set of the set to update the set, and calls the difference_update() method.

If A and B are two groups of sets. The difference set of A and B is a set of elements that only exist in set A but not in set B.

For more information, please visitPython set difference.

The syntax of Difference_update() is:

A.difference_update(B)

Here, A and B are two sets. Difference_update() uses A-B's set difference updates the A set.

Difference_update() return value

difference_update() returns None, indicating that the object (set) has changed.

Assuming

result = A.difference_update(B)

When you run the code

  • result will be None

  • A will be equal to A-B

  • B will remain unchanged

Example: How does difference_update() work?

A = {'a', 'c', 'g', 'd'}
B = {'c', 'f', 'g'}
result = A.difference_update(B)
print('A = ', A)
print('B = ', B)
print('result = ', result)

When running the program, the output is:

A =  {'d', 'a'}
B =  {'c', 'g', 'f'}
result =  None

Méthodes de collection en Python