Skip to content

What is Python set intersection operator | Code

  • by

Set intersection means getting a new set with elements that are common to all sets. In Python intersection has & operator. You can also use the intersection() method to intersect two or more sets:

Note: The set intersection operator (&) will raise an error if you use it with iterables.

Example use set intersection operator in Python

Simple example code.

set1 = {'Python', 'Java', 'C++'}
set2 = {'C#', 'Java', 'C++'}

res = set1 & set2

print(res)

Output:

What is Python set intersection operator

Do comment if have any questions or suggestions on this Python operator topic.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.