Python Glossary Русский

all Python 3Sets (set)function

frozenset.difference

frozenset

Return a new frozenset — the difference (elements not in the others).

Syntax

f.difference(*others)

The difference from the minus operator is what it accepts: f - [1, 2] raises TypeError, while f.difference([1, 2]) takes any iterable, and as many of them as you like. The operation is not symmetric — f.difference(g) and g.difference(f) differ; for "in one or the other but not both" use symmetric_difference. There is no difference_update on frozenset: nothing can be changed in place, so you always get a new object.

Examples

print(frozenset({1, 2, 3}).difference({2}))   # → frozenset({1, 3})
print(frozenset({1, 2, 3, 4}).difference({2}, {4}))   # → frozenset({1, 3})
print(frozenset({1, 2, 3}).difference([1, 2]))   # → frozenset({3})
print(sorted(frozenset('hello').difference('aeiou')))   # → ['h', 'l']
print(frozenset({1, 2}).difference({1, 2}))   # → frozenset()
print(frozenset({1, 2, 3}) - [1])   # → TypeError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy