Python set.intersection() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
set method

Definition

intersection(*others) builds a new set with only the items that appear in the set and in every iterable you pass. The original set is not changed.

If nothing is shared the result is an empty set, which Python prints as set(). With no arguments, you simply get a copy of the set.

set.intersection() is an instance method of the built-in set type: call it on a set with dot notation, such as skills.intersection(...). No import is needed.

Syntax

Pass one or more iterables, and keep the returned set in a variable.

python
common = s.intersection(iterable)
common = s.intersection(iterable1, iterable2)
common = s & other_set
CallWhat it does
{1, 2, 3}.intersection({2, 3, 4})Returns {2, 3}, a new set of the items found in both.
{1, 2, 3}.intersection([3, 4], {3, 5})Returns {3}: only 3 is in the set and in every iterable.
{1, 2}.intersection({3})Returns set(): nothing is shared, so the result is an empty set.

a & b is a shortcut for a.intersection(b), but only between two sets. To shrink the existing set instead of making a new one, use intersection_update().

Method signatures
python
set.intersection(*others)
# returns: a new set with the items present in the set and in every iterable
# raises: TypeError if an argument is not iterable or an item is unhashable

Parameters

ParameterDescription
*othersZero or more iterables: set, list, tuple, string, range, and so on. An item is kept only if it appears in all of them.

Return Value

Return ValueDescription
setA new set with the shared items. It is empty (set()) when nothing is shared.

Python Examples

Example 1 — Find the Skills Two Candidates Share

Intersect two skill sets and compare with the & operator.

python
dana = {"python", "sql", "git", "docker"}
eli = {"sql", "java", "git"}

shared = dana.intersection(eli)
print(sorted(shared))
print(len(shared))
print(sorted(dana & eli) == sorted(shared))

How It Works

  1. Only the overlap. Git and SQL are the only skills that both candidates have, so the result has two items.
  2. A new set. shared is a separate set, so Dana's and Eli's skills are unchanged.
  3. The operator agrees. dana & eli finds the same two skills, so the comparison prints True.

Example 2 — Find the Days When Everyone Is Free

Pass a list and a tuple along with the set, and see what an empty result looks like.

python
ann = {"mon", "tue", "wed", "thu"}
bob = ["tue", "wed", "fri"]
cy = ("wed", "thu", "tue", "sat")

free = ann.intersection(bob, cy)
print(sorted(free))
print(sorted(ann.intersection(["sun"])))
print(ann.intersection(["sun"]))

How It Works

  1. Every argument must agree. Tuesday and Wednesday are the only days present in Ann's set, Bob's list, and Cy's tuple.
  2. Nothing in common. Nobody lists Sunday as free in Ann's set, so the result has no items and sorts to [].
  3. The empty set. Printing the result directly shows set().

Example 3 — Keep Only Valid Country Codes

Intersect typed input with the list of valid codes, and find the rejected codes with a set difference.

python
valid = {"US", "IN", "DE", "FR", "JP"}
entered = ["IN", "XX", "US", "IN", "ZZ"]

accepted = valid.intersection(entered)
print(sorted(accepted))

typed = set(entered)
print(sorted(typed - valid))
print(accepted == valid & typed)

How It Works

  1. Duplicates do not matter. "IN" was typed twice but appears once in the result.
  2. A list works as an argument. intersection() accepts the typed list directly, with no conversion needed.
  3. Rejected codes by subtraction. Converting the input to a set and subtracting the valid codes leaves "XX" and "ZZ".
  4. Same as the operator. valid & typed produces an equal set, so the last line prints True.

Frequently Asked Questions

A new set with only the items that appear in the set and in every argument. If nothing is shared, it returns an empty set, printed as set().
Yes for two sets, but the & operator requires sets on both sides. The method also accepts lists, tuples, strings, and several arguments.
intersection() returns a new set and leaves the original alone. intersection_update() shrinks the original set in place and returns None.
Use isdisjoint(), which returns True when there is no shared item. It is faster than building the whole intersection.
Yes. Pass them all to one call, as in a.intersection(b, c), or chain operators, as in a & b & c. Only items present in all of them are kept.

Next: set.intersection_update()

Keep only the items that also appear in every other iterable, in place.

intersection_update() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful