Python Glossary Русский

all Python 3Sets (set)function

frozenset.intersection

frozenset

Return a new frozenset — the intersection with the others.

Syntax

f.intersection(*others)

When both sides are sets, CPython walks the smaller one and tests membership in the larger, so the cost scales with the smaller set, not with the total. Unlike the & operator, the method takes any iterable, and several at once: f.intersection(a, b) intersects with both. If you only need to know whether anything overlaps, isdisjoint is cheaper — it stops at the first common element instead of building a whole new set.

Examples

print(frozenset({1, 2, 3}).intersection({2, 3}))   # → frozenset({2, 3})
print(frozenset({1, 2, 3, 4}).intersection({2, 3, 4}, {3, 4, 5}))   # → frozenset({3, 4})
print(frozenset({1, 2, 3}).intersection([3, 4, 5]))   # → frozenset({3})
print(sorted(frozenset('hello').intersection('world')))   # → ['l', 'o']
print(frozenset({1, 2}).intersection({3, 4}))   # → frozenset()
print(frozenset({1, 2, 3}) & [2, 3])   # → TypeError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy