Python Set Methods

Beginner
5 min read
Updated: Nov 2026
17 method guides
Method index

About This Index

This page is the Python Set Methods index for the tutorial track. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. A set has no fixed order, so every example prints sorted() results, and the output you see is the output you get.

Learn creating sets, the set rules, set math, and the classic mistakes on the Set hub. Use this page when you need a specific method such as add(), discard(), or union().

Seventeen methods, one type.

Every method below belongs to the built-in set type, so no import is needed. Nine of them change the set in place; of those, only pop() hands something back, and the rest return None. The other eight leave the set alone and return a new set or True / False. The Returns column tells you which is which.

How to Call a Set Method

Write the set, a dot, the method name, and the arguments. Methods that change the set return None, so call them on their own line; union(), issubset(), and the other methods that answer a question give you a value to use:

Python
tags = {"python", "sets"}
tags.add("loops")                 # add one item
tags.update(["sql", "git"])       # add many items
tags.discard("git")               # remove; never raises
print(sorted(tags))               # ['loops', 'python', 'sets', 'sql']
more = tags.union({"c"})          # a new set; tags is unchanged
print(len(tags), len(more))       # 4 5
print(tags.issubset(more))        # True

Python Set Methods Table

All method guides in this Python tutorial series (17 tutorials), grouped by purpose.

Add items

2 methods

Put one item into the set, or every item of one or more iterables, in place. Duplicates are always ignored.

MethodReturnsDescriptionTutorial
add()NoneAdd one item to the set, in place; an item already there changes nothing.Try it
update()NoneAdd every item of one or more iterables to the set, in place.Try it

Remove items

4 methods

Delete a chosen item (with or without an error for a missing one), take out an arbitrary item and get it back, or empty the whole set in place.

MethodReturnsDescriptionTutorial
remove()NoneDelete an item from the set; raises KeyError if the item is missing.Try it
discard()NoneDelete an item if it is in the set; do nothing if it is not.Try it
pop()the removed itemRemove and return an arbitrary item; raises KeyError if the set is empty.Try it
clear()NoneRemove every item from the set, in place.Try it

Combine sets into a new set

4 methods

Merge, overlap, subtract, or compare sides, and get a brand-new set while the originals stay as they are.

MethodReturnsDescriptionTutorial
union()set (new)Return a new set with every item from the set and the other iterables.Try it
intersection()set (new)Return a new set with only the items found in the set and in every other iterable.Try it
difference()set (new)Return a new set with the items of the set that are not in any other iterable.Try it
symmetric_difference()set (new)Return a new set with the items that are in exactly one of the two sets.Try it

Change the set in place

3 methods

The same set math as above, but the result is written back into the set itself and the method returns None.

MethodReturnsDescriptionTutorial
intersection_update()NoneKeep only the items that also appear in every other iterable, in place.Try it
difference_update()NoneRemove every item found in the other iterables from the set, in place.Try it
symmetric_difference_update()NoneKeep the items that are in exactly one of the set and the other iterable, in place.Try it

Compare and copy

4 methods

Ask whether two sets overlap, contain each other, or make an independent copy of a set.

MethodReturnsDescriptionTutorial
isdisjoint()boolReturn True if the set and the other iterable share no items.Try it
issubset()boolReturn True if every item of the set is also in the other iterable.Try it
issuperset()boolReturn True if the set contains every item of the other iterable.Try it
copy()set (new)Return a new set with the same items.Try it

Set Operators and Idioms

These are not named methods, but you will use them next to the methods above in almost every program:

CodeWhat it does
x in sTrue if x is in the set. Very fast, even for huge sets.
len(s)Number of items.
a | bNew set with the items of both; like a.union(b), but both sides must be sets.
a & bNew set with the items found in both; like a.intersection(b).
a - bNew set with the items of a that are not in b; like a.difference(b).
a ^ bNew set with the items that are in exactly one of them; like a.symmetric_difference(b).
a <= bTrue if every item of a is in b, like a.issubset(b). a < b also needs b to have extra items.
a >= bTrue if a contains every item of b, like a.issuperset(b). a > b also needs a to have extra items.
a |= bAdd the items of b in place, like a.update(b), but b must be a set.
a &= bKeep only the shared items, in place, like a.intersection_update(b).
a -= bRemove the items of b, in place, like a.difference_update(b).
a ^= bToggle the items of b, in place, like a.symmetric_difference_update(b).
{x for x in items}Set comprehension: build a set from any loop, dropping duplicates.
sorted(s)New sorted list of the items; the usual way to print a set in a fixed order.
frozenset(s)An unchangeable set, which can itself be an item of another set or a dict key.
Python
a = {1, 2, 3, 4}
b = {3, 4, 5}
print(sorted(a | b))              # [1, 2, 3, 4, 5]
print(sorted(a & b))              # [3, 4]
print(sorted(a - b))              # [1, 2]
print(sorted(a ^ b))              # [1, 2, 5]
print({3, 4} <= a, a < b)         # True False
print(2 in a, len(a))             # True 4
a |= {9}                          # same as a.update({9})
print(sorted(a))                  # [1, 2, 3, 4, 9]

Pick the method form, such as union() or update(), when the other side is a list, tuple, or string, or when you pass several iterables, and the operator form when both sides are sets. Use intersection() or a & b to get a new set, and intersection_update() or a &= b to change a itself.

Frequently Asked Questions

They are the functions you call on a set with dot notation, such as tags.add("x") or a.union(b). They add, remove, combine, compare, or copy items.
This index covers all 17 public set methods: add(), clear(), copy(), difference(), difference_update(), discard(), intersection(), intersection_update(), isdisjoint(), issubset(), issuperset(), pop(), remove(), symmetric_difference(), symmetric_difference_update(), union(), and update().
add(), update(), remove(), discard(), pop(), clear(), and the three _update() methods change the set in place. union(), intersection(), difference(), symmetric_difference(), and copy() return a new set, while isdisjoint(), issubset(), and issuperset() return True or False.
They give the same result for two sets, but the operators need a set on both sides. The methods also accept lists, tuples, strings, and several arguments at once.
A set has no order, so the items may appear in any order, and the order can change between runs for strings. Use sorted(s) when you need a stable order.

Start with add()

Open the first guide in the set and work your way through the set methods.

add() tutorial →

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