Python Set symmetric_difference() Method

The set.symmetric_difference() method returns a new set with the distinct elements found in both the sets.

Syntax:

set.symmetric_difference(other_set)

Parameters:

other_set: Required. The set with which the symmetric difference is to be determined.

Return Value:

Returns the symmetric difference of the sets.

The following example demonstrates the set.symmetric_difference() method.

Example:
nums1 = {1,2,3,4,5}
nums2 = {4,5,6,7,8}
nums3 = nums1.symmetric_difference(nums2)
print("New set: ", nums3)
Output
New set: {1, 2, 3, 6, 7, 8}

The ^ operator can also be used to find the symmetric difference of sets.

Example:
nums1 = {1,2,3,4,5}
nums2 = {4,5,6,7,8}
nums3 = nums1 ^ nums2
print("New set: ", nums3)
Output
New set: {1, 2, 3, 6, 7, 8}
Want to check how much you know Python?