Python Set symmetric_difference() method with Examples

The Python set symmetric_difference() function of the Set class returns a symmetric difference between two sets. The items that are in either set X or set Y but not both are contained in the symmetric difference of two sets X and Y. Considering elements 2 and 3 are present in both sets, the symmetric difference of sets {1, 2, 3} and {2, 3, 4} would equal {1, 4}.

Python Set symmetric_difference() Syntax

set.symmetric_difference(another_set)
  • Parameter: It takes a set as a parameter
  • Return Value: It returns a new set which is a symmetric difference between the two given sets.

Now Let’s have a look at coding examples:

Example 01: Using Python Set symmetric_difference() Method

We have three sets in the following example: X, Y, and Z. The sets Y and Z are identical. As illustrated in the result of the following example, finding the symmetric difference between the same sets returns nothing. The symmetric difference may also be found using the operator, which is covered in the next part of this article.

# Set X
X = {1, 2, 3}

# Set Y
Y = {2, 3, 4}

# Set Z
Z = {2, 3, 4}

print("Symmetric difference between X & Y", X.symmetric_difference(Y))
print("Symmetric difference between Y & Z", Y.symmetric_difference(Z))
print("Symmetric difference between X & X", X.symmetric_difference(X))

Output:

Python Set symmetric_difference() Method

Example 02: Using the Symmetric Difference Operator (^) Method

To get the symmetric difference between two sets, we may use the ^ operator instead of the symmetric difference() method, as shown in the following example.

# Set X
X = {1, 2, 3}

# Set Y
Y = {2, 3, 4}

# Set Z
Z = {2, 3, 4}

print(X^Y)
print(X^Z)
print(Y^Z)

# symmetric difference with self
print(X^X)
print(Y^Y)

Output:

Symmetric Difference Operator (^) Method

FAQs

What is the difference between difference and symmetric difference in Python?

Difference: Elements are present on one set, but not on the other.
Symmetric Difference: Elements from both sets, that are not present on the other.

How do you check for symmetry in Python?

If the string length is even in the case of symmetry, the string is split into two halves and the loop is executed, verifying the characters of both halves. The loops break if the letters are not comparable, and the string is not symmetrical; otherwise, the string is symmetrical.

How do you find subsets in Python?

In Python, you can use the Set issubset() method to check if a set is a subset of another:

  • Set A is a subset of set B if all elements of set A are also elements of the set B
  • Use the Set issubset() method to return True if a set is a subset of another set.
  • Also, use the subset operator (<=) or the proper subset operator (<) to check if a set is a subset or proper subset of another set.

Conclusion of Python Set symmetric_difference()

That’s all for this article. If you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn.

Suggested Articles:

  1. Python Strip Function – strip() [With Examples]

Leave a Comment