how to get the symmetric difference of two sets in python

To get the symmetric difference of two sets in Python, you can use the "^" operator or the "symmetric_difference()" method.

Using the "^" operator:

main.py
set_1 = {1, 2, 3, 4}
set_2 = {3, 4, 5, 6}
symmetric_diff = set_1 ^ set_2
print(symmetric_diff)  # {1, 2, 5, 6}
111 chars
5 lines

Using the "symmetric_difference()" method:

main.py
set_1 = {1, 2, 3, 4}
set_2 = {3, 4, 5, 6}
symmetric_diff = set_1.symmetric_difference(set_2)
print(symmetric_diff)  # {1, 2, 5, 6}
131 chars
5 lines

gistlibby LogSnag