Python 3 reference · Set Methods
Python Set intersection() Method
Return a new set with the elements common to this set and all others. The same as a & b.
Python 3 Since 3.0
Syntax
set.intersection(*others)
Parameters
| Parameter | Description |
|---|---|
*others |
One or more iterables. |
Return value
A new set.
Examples
python = {"Ada", "Bob", "Cy"}
java = {"Bob", "Dee"}
print(python.intersection(java), python & java)
Output
{'Bob'} {'Bob'}a, b, c = {1, 2, 3}, {2, 3, 4}, {3, 4, 5}
print(a.intersection(b, c))
Output
{3}Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
set.intersection_update()· Keep only the elements found in this set and all others, in place. The same as a &= b.set.union()· Return a new set with all elements from this set and the others. The same as a | b.set.isdisjoint()· Return True if the set has no elements in common with another iterable.- Python Sets · tutorial