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

Python basic tutorial

Python flow control

Fonction 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 collection symmetric_difference() usage and example

Méthodes de collection en Python

The Python symmetric_difference() method returns the symmetric difference sets of two groups.

The symmetric difference of two sets A and B is the set of elements that are in A or B, but not at their intersection.

The syntax of symmetric_difference() is:

A.symmetric_difference(B)

Example1The work of :symmetric_difference()

A = {'a', 'b', 'c', 'd'}
B = {'c', 'd', 'e'}
C = {}
print(A.symmetric_difference(B))
print(B.symmetric_difference(A))
print(A.symmetric_difference(C))
print(B.symmetric_difference(C))

Output result

{'b', 'a', 'e'}
{'b', 'e', 'a'}
{'b', 'd', 'c', 'a'}
{'d', 'e', 'c'}

Symmetric difference set using ^ operator

In Python, we can also use the ^ operator to find the symmetric difference set.

A = {'a', 'b', 'c', 'd'}
B = {'c', 'd', 'e'}
print(A ^ B)
print(B ^ A)
print(A ^ A)
print(B ^ B)

Output result

{'e', 'a', 'b'}
{'e', 'a', 'b'}
set()
set()

Méthodes de collection en Python