set.difference
Find the items of the set that are not in the other collections.
Python 3.13
set.difference(*others)Parameters
- *othersIterable
- The collections whose items are excluded.
Return value
A new set; the originals are not changed.
Examples
Items only in the first set
Python 3.13
print({1, 2, 3}.difference({2, 3, 4})){1}The order of the sets matters
Python 3.13
a = {1, 2, 3}
b = {2, 3, 4}
print(a.difference(b), b.difference(a)){1} {4}Details
For two sets, the - operator does the same. The order matters: a.difference(b) and b.difference(a) give different results.
