W3docs

Python Set Methods – Complete Reference with Examples

Complete guide to Python set methods: union, intersection, difference, update, add, remove, discard, copy, isdisjoint, and more with examples.

Python sets are unordered collections of unique, hashable elements. Because sets implement the mathematical concept of a set, they come with a rich collection of built-in methods for combining, comparing, and modifying them. This chapter covers every built-in set method with clear examples, expected output, and guidance on when to choose one method over another.

If you are new to sets, read Python Sets first to understand how sets are created and why they differ from lists and tuples.

Creating sets

Use curly braces {} or the set() constructor to create a set. The constructor also accepts any iterable — a list, tuple, or string — and automatically removes duplicates.

fruits = {'apple', 'banana', 'cherry'}
colors = set(['red', 'green', 'blue'])   # from a list
vowels = set('aeiou')                    # from a string → {'a', 'e', 'i', 'o', 'u'}

empty = set()   # NOT {} — that creates an empty dict

Sets are unordered: the printed order of elements may differ between runs and Python versions.

Set operation methods

These methods combine or compare two or more sets without modifying the originals.

union()

union(*others) returns a new set containing every element that appears in the current set or in any of the others.

python— editable, runs on the server

The | operator is the equivalent shorthand for two sets: A | B.

When to use: merging several collections while deduplicating elements in a single step.

intersection()

intersection(*others) returns a new set containing only the elements that appear in the current set and in every one of the others.

python— editable, runs on the server

The & operator is the shorthand for two sets: A & B.

When to use: finding common elements — for example, users who clicked both ad A and ad B.

difference()

difference(*others) returns a new set of elements that are in the current set but not in any of the others.

python— editable, runs on the server

The - operator is the shorthand: A - B.

When to use: filtering one group out of another — for example, finding products that are in stock but not yet shipped.

symmetric_difference()

symmetric_difference(other) returns a new set of elements that appear in exactly one of the two sets — the elements that are not shared.

python— editable, runs on the server

The ^ operator is the shorthand: A ^ B.

When to use: detecting what changed between two snapshots — for example, files added or deleted between two directory listings.

issubset()

issubset(other) returns True if every element of the current set is also in other.

python— editable, runs on the server

The <= operator is equivalent; use < to test a proper subset (A is smaller than B and fully contained).

issuperset()

issuperset(other) returns True if the current set contains every element of other.

A = {1, 2, 3, 4, 5}
B = {2, 3}

print(A.issuperset(B))   # True
print(B.issuperset(A))   # False

The >= operator is equivalent; use > to test a proper superset.

isdisjoint()

isdisjoint(other) returns True if the two sets have no elements in common — their intersection is empty.

A = {1, 2, 3}
B = {4, 5, 6}
C = {3, 4, 5}

print(A.isdisjoint(B))   # True  — no overlap
print(A.isdisjoint(C))   # False — 3 is in both

When to use: validating non-overlapping categories, checking that two permission sets are mutually exclusive.

In-place update methods

These methods modify the set in place and return None — they do not create a new set.

update()

update(*others) adds all elements from one or more iterables into the current set, ignoring duplicates.

python— editable, runs on the server

update() accepts any iterable, not just another set — a list, tuple, or string works too. The |= operator is the shorthand for two sets.

intersection_update()

intersection_update(*others) keeps only the elements that are also in every one of the others, discarding everything else.

A = {1, 2, 3, 4}
B = {2, 3, 4, 5}

A.intersection_update(B)
print(A)   # {2, 3, 4}

The &= operator is the shorthand.

difference_update()

difference_update(*others) removes from the current set every element that appears in any of the others.

A = {1, 2, 3, 4}
B = {3, 4, 5}

A.difference_update(B)
print(A)   # {1, 2}

The -= operator is the shorthand.

symmetric_difference_update()

symmetric_difference_update(other) retains only the elements that appear in exactly one of the two sets, updating in place.

A = {1, 2, 3}
B = {2, 3, 4}

A.symmetric_difference_update(B)
print(A)   # {1, 4}

The ^= operator is the shorthand.

Element manipulation methods

add()

add(elem) inserts elem into the set. If elem is already present, the set is unchanged.

python— editable, runs on the server

See Adding Set Items for more ways to populate a set.

remove()

remove(elem) removes elem from the set. Raises KeyError if elem is not present.

python— editable, runs on the server

Use remove() when the element must be in the set and an unexpected absence is a programming error worth surfacing.

discard()

discard(elem) removes elem from the set. Does not raise an error if elem is absent.

python— editable, runs on the server

Use discard() when the element may or may not be in the set and you just want it gone.

pop()

pop() removes and returns an arbitrary element. Because sets are unordered, you cannot predict which element is removed. Raises KeyError if the set is empty.

python— editable, runs on the server

See Removing Set Items for a comparison of all removal approaches.

clear()

clear() removes every element, leaving an empty set.

python— editable, runs on the server

Note that print(set()) displays set(), not {}, to distinguish an empty set from an empty dict.

copy()

copy() returns a shallow copy of the set. Modifying the copy does not affect the original.

A = {1, 2, 3}
B = A.copy()

B.add(4)
print(A)   # {1, 2, 3}  — unchanged
print(B)   # {1, 2, 3, 4}

This is important because a plain assignment (B = A) creates a second reference to the same set — changes to B would also affect A.

Method vs. operator: which to prefer?

Most set operations have both a method form and an operator form:

OperationMethodOperator
UnionA.union(B)A | B
IntersectionA.intersection(B)A & B
DifferenceA.difference(B)A - B
Symmetric differenceA.symmetric_difference(B)A ^ B
In-place unionA.update(B)A |= B
In-place intersectionA.intersection_update(B)A &= B
In-place differenceA.difference_update(B)A -= B
In-place symmetric diffA.symmetric_difference_update(B)A ^= B
SubsetA.issubset(B)A <= B
SupersetA.issuperset(B)A >= B

Key difference: the methods accept any iterable as an argument, while the operators require both sides to be sets. Use methods when you want to operate on a list or tuple directly without converting it first:

tags = {'python', 'web'}
new_tags = ['python', 'ml', 'data']

tags.update(new_tags)         # works — list is fine
# tags |= new_tags            # TypeError — operator needs a set
print(tags)   # {'python', 'web', 'ml', 'data'}

Complete set methods reference

MethodDescription
add(elem)Adds elem to the set; no effect if already present.
clear()Removes all elements, leaving an empty set.
copy()Returns a shallow copy of the set.
difference(*others)Returns a new set with elements not in any of the others.
difference_update(*others)Removes from the set all elements found in others.
discard(elem)Removes elem if present; no error if absent.
intersection(*others)Returns a new set of elements common to the set and all others.
intersection_update(*others)Keeps only elements found in the set and all others.
isdisjoint(other)Returns True if the set and other share no elements.
issubset(other)Returns True if every element of the set is in other.
issuperset(other)Returns True if the set contains every element of other.
pop()Removes and returns an arbitrary element; raises KeyError if empty.
remove(elem)Removes elem; raises KeyError if not present.
symmetric_difference(other)Returns elements in exactly one of the two sets.
symmetric_difference_update(other)Updates the set to the symmetric difference in place.
union(*others)Returns a new set with all elements from the set and all others.
update(*others)Adds all elements from others to the set.

Practice

Practice
Which of the following are methods that can be used on sets in Python?
Which of the following are methods that can be used on sets in Python?
Was this page helpful?