set.difference(*others) returns a new set with the elements of the first set that do not appear in any of the other arguments. It is the same operation as the - operator, and it leaves the original sets unchanged.
Use it to find what is unique to one collection: users who have not logged in, files missing from a backup, products one store sells and another does not.
set. set_a is not modified.set_a.Set difference is not symmetric: a - b and b - a give different results. Sets are unordered, so the order in which the elements print can differ from what you expect.
The two forms give the same result for sets, with one important distinction.
| Form | Accepts | Number of operands |
|---|---|---|
a.difference(x, y) | Any iterable (list, tuple, string, generator) | One or more |
a - b | Sets and frozensets only | Two per operator, chain for more |
The method converts the other iterables internally, so you do not have to build a set first. The operator is shorter when both sides are already sets.
Pass multiple arguments to subtract more than one set in a single call.
Wrapping the result in sorted() makes the output deterministic, which is useful in tests.
A common pattern is to compute the work that is left to do.
Normalise the values before comparing, because "Apple" and "apple" are different set elements.
difference_update() modifies the set instead of returning a new one. The augmented operator -= does the same for sets.
difference_update() returns None, so do not assign its result. Use discard() or remove() when you are removing a single known element.
| Goal | Method | Operator |
|---|---|---|
| Elements in both | intersection() | & |
| Elements in either | union() | | |
| Elements in exactly one | symmetric_difference() | ^ |
| Elements only in the first | difference() | - |
Set elements must be hashable, so you can build sets of numbers, strings and tuples but not of lists or dictionaries. Membership tests and set operations run in roughly linear time in the size of the inputs, which makes difference() much faster than filtering one list against another with a nested loop.
No. It returns a new set and leaves the original untouched. Use difference_update() or -= if you want to modify the set in place.
Yes for the method, because its arguments can be any iterable, but the object you call it on must be a set. The - operator needs sets on both sides.
No. A set stores each element once and has no guaranteed order. If you need to keep order or duplicates, filter a list with a comprehension against a set, for example [x for x in items if x not in blocked].
a.difference(b) returns elements only in a. a.symmetric_difference(b) returns elements that are in exactly one of the two sets.
set.difference() gives you the elements of one set that are missing from others, without changing the original. The method accepts any iterables and several arguments, while - works only between sets. Use difference_update() or -= to modify a set in place, and sort the result when you need stable output.