Home / Python / Reference / Set methods

Python set intersection() method

Return the items in both sets.

Syntax

set.intersection(*others) / a & b

Parameters

*othersother sets or iterables

Return value

a new set

Example

Code

python_class = {1, 2, 3, 4}
sql_class = {3, 4, 5}
print(python_class & sql_class)
print(sorted({"Sita", "Leo", "Mia"} & {"Leo", "Mia", "Zoe"}))

Output

{3, 4}
['Leo', 'Mia']

Related

Learn it step by step in the free lesson Dictionaries & Sets, with exercises that check your answers.