Python Tutorial - Master Python Programming For Beginners from Scratch

  • Home
  • Getting Started
  • Basics
  • OOP
  • Advanced Python
  • Tkinter
Home » Python Basics » Python Set Intersection

Python Set Intersection

Summary: in this tutorial, you’ll learn about the Python set intersection and how to use it to intersect two or more sets.

TL;DR

In Python, you can use the Set intersection() method or set intersection operator (&) to intersect two or more sets:

new_set = set1.intersection(set2, set3) new_set = set1 & set2 & set3

Introduction to Python set intersection

When intersecting two or more sets, you’ll get a new set consisting of elements that exist in all sets.

Suppose that you have two following sets s1 and s2:

s1 = {'Python', 'Java','C++'} s2 = {'C#', 'Java', 'C++' }
Code language: JavaScript (javascript)

The intersection of these two sets returns a new set that contains two elements 'Java' and 'C++':

s = {'Java', 'C++'}
Code language: JavaScript (javascript)

… because they’re only elements that exist in both sets.

The following Venn diagram illustrates the intersection of two sets s1 and s2:

Python Set Intersection Example

The set intersection has many useful applications. For example, you can use set intersections to find the common favorites of two friends on a social networking application or to search for common skills of two or more employees on an HR application.

In Python, you can intersect two or more sets using the set intersection() method or set intersection operator (&).

1) Using Python set intersection() method to intersect two or more sets

This shows how to use the Set intersection() method to intersect two or more sets:

new_set = set1.intersection(set2, set3, ...)

The following shows how to use the intersection() method to intersect the sets s1 and s2:

s1 = {'Python', 'Java', 'C++'} s2 = {'C#', 'Java', 'C++'} s = s1.intersection(s2) print(s)
Code language: PHP (php)

Output:

{'C++', 'Java'}
Code language: JavaScript (javascript)

2) Using Python set intersection (&) operator to intersect two or more sets

Python provides you with the set intersection operator (&) that allows you to intersect two or more sets:

new_set = s1 & s2 & s3 & ...

The following example uses the set intersection operator (&) to intersect the sets s1 and s2:

s1 = {'Python', 'Java', 'C++'} s2 = {'C#', 'Java', 'C++'} s = s1 & s2 print(s)
Code language: PHP (php)

Output:

new_set = s1 & s2 & s3 & ...

Set intersection() method vs set intersection operator (&)

The set intersection operator only allows sets while the set intersection() method can accept any iterables, like strings, lists, and dictionaries.

If you pass iterables to the intersection() method, it’ll convert the iterables to set before intersecting them.

However, the set intersection operator (&) will raise an error if you use it with iterables.

The following example uses the intersection() method to intersect a set with a list:

numbers = {1, 2, 3} scores = [2, 3, 4] numbers = numbers.intersection(scores) print(numbers)
Code language: PHP (php)

Output:

{2, 3}

If you use the set intersection operator (&) instead, you’ll get an error:

numbers = {1, 2, 3} scores = [2, 3, 4] numbers = numbers & scores print(numbers)
Code language: PHP (php)

Output:

TypeError: unsupported operand type(s) for &: 'set' and 'list'
Code language: JavaScript (javascript)

Summary

  • The intersection of two or more sets returns elements that exist in all sets.
  • Use the intersection() method or set intersection operator (&) to intersect two or more sets.
  • Was this tutorial helpful ?
  • YesNo
Previous Python Set Union
Next Python Set Difference

Getting Started

  • What is Python
  • Install Python
  • Setup VS Code for Python
  • Develop Python Hello World Program

Python Fundamentals

  • Python Syntax
  • Variables
  • Strings
  • Numbers
  • Boolean
  • Constants
  • Comments
  • Type Conversion

Control Flow

  • if…else
  • Ternary Operator
  • for Loop
  • while Loop
  • break
  • continue
  • pass

Functions

  • Python Functions
  • Default Parameters
  • Keyword Arguments
  • Recursive Functions
  • Lambda Expressions
  • Function Docstrings

Python List

  • List
  • Tuple
  • Sorting a List in Place: sort()
  • Sorting a List: sorted()
  • Slicing a List: [::]
  • Unpacking a List
  • Iterating over a List: for loop
  • Finding Index of an Element: index()
  • Iterables
  • Transform List Elements: map()
  • Filtering List Elements: filter()
  • Reducing List Elements: reduce()
  • List Comprehensions

Python Dictionary

  • Dictionary
  • Dictionary Comprehension

Python Set

  • Set
  • Set Comprehension
  • Union of Sets
  • Intersection of Sets
  • Difference between Sets
  • Symmetric Difference of Sets
  • Subset
  • Superset
  • Disjoint Sets

Exception Handling

  • try…except
  • try…except…finally
  • try…except…else

Python Loop with Else Clause

  • for…else
  • while…else
  • do…while Emulation

More on Functions

  • Unpacking Tuples
  • The *args Parameters
  • The **kwargs Parameters
  • Partial Functions

Modules

  • Modules
  • Module Search Path
  • Python __name__
  • Packages

File I/O

  • Reading a Text File
  • Writing to a Text File
  • Creating a Text File
  • Checking If a File Exists
  • Reading from a CSV File
  • Writing to a CSV File

Managing Third-party Packages

  • Python Package Index (PyPI) & PIP
  • Python Virtual Environments
  • Install pipenv on Windows
  • Using pipenv Tool

About pythontutorial.net

Pythontutorial.net helps you master Python programming from scratch fast.

Site Links

  • Home
  • Contact
  • About
  • Privacy Policy

Recent Python Tutorials

  • Tkinter StringVar
  • Tkinter PhotoImage
  • Tkinter OptionMenu
  • Tkinter ScrolledText
  • How to Change the Appearances of Widgets Dynamically Using Ttk Style map() Method

Python References

  • Python String Methods
  • Python MySQL
  • Python PostgreSQL
  • Python Oracle
  • Python SQLite

Copyright © 2021 · by pythontutorial.net. All rights reserved. Log in