In Python, we can get the difference of two sets by using the '-' operator or by calling the difference()
method on one of the sets.
Here's an example using the difference()
method:
main.py110 chars7 lines
And here's an example using the '-' operator:
main.py100 chars7 lines
Both of these approaches will result in a new set that contains the elements that are in set1
but not in set2
. If you want to get the symmetric difference (i.e. the elements that are in one set but not the other), you can use the symmetric_difference()
method:
main.py137 chars7 lines
In this case, the resulting set contains all elements that are in set1
or set2
but not in both.
gistlibby LogSnag