Python Glossary Русский

all Python 3Sets (set)function

set.intersection

set theory

Return a new set of elements common to all (intersection). Operator: &.

Syntax

s.intersection(*others)

The method takes any iterables; the & operator takes sets only. When all you need is whether two collections overlap at all, isdisjoint() answers that without building a whole new set.

Examples

print(sorted({1, 2, 3}.intersection({2, 3, 4})))   # → [2, 3]
print(sorted({1, 2, 3}.intersection({2, 3}, [3, 9])))   # → [3]
print(sorted({1, 2, 3} & {3, 4}))   # → [3]
print(sorted(set('python').intersection('typo')))   # → ['o', 'p', 't', 'y']
print({1, 2}.intersection({3}))   # → set()
print({1, 2} & [2])   # → TypeError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy