A set in Python is an unordered collection of unique items. Sets automatically remove duplicates, check membership very quickly, and support mathematical operations such as union, intersection, and difference.
Unlike a list, a set doesn’t keep items in a fixed order, so you can’t access items by index. In this tutorial, you’ll learn how to create sets, add and remove items, use every set operation, and choose between a set and other data types.
Also See:

Characteristics of a set
- Unordered: Items have no index or position. The order you see when printing a set is arbitrary, so don’t rely on it.
- Unique: A set can’t contain the same value twice. Duplicates are dropped automatically.
- Mutable, but with immutable items: You can add and remove items, but each item must be hashable (immutable), such as a number, string, or tuple. Lists, dictionaries, and other sets can’t be set items.
Table of contents
Creating a set
There are two ways to create a set:
- Curly braces: put comma-separated items inside
{}. - The
set()constructor: pass any iterable, such as a list, tuple, string, or range.
Output:
{25, 'Mark', 75.25, 'Jessa'}
<class 'set'>
['Angels and Demons', 'Atlas Shrugged', 'Harry Potter']
Because a set’s order is arbitrary, this tutorial often wraps a set in sorted() so the printed output matches what you’ll see.
Create a set from a list (remove duplicates)
Passing a list to set() keeps only one copy of each value. It’s the quickest way to remove duplicates:
Sets can’t contain mutable items
Set items must be hashable. Trying to put a list (or dictionary, or set) inside a set raises a TypeError. Use a tuple instead of a list if you need a group of values as one item.
Empty set
Empty curly braces {} create an empty dictionary, not a set. Use set() for an empty set:
Accessing items of a set
Sets have no index, so book_set[0] raises a TypeError. To read the items, loop over the set with a for loop:
Output:
Angels and Demons Atlas Shrugged Harry Potter
Check if an item exists
Use the in and not in operators. Membership tests are where sets shine: they take about the same time no matter how big the set is, while a list has to be scanned item by item.
Find the length of a set
Adding items to a set
add(item)adds one item. Adding an item that’s already there does nothing.update(iterable, ...)adds every item from one or more iterables.
Output:
5 ['Angels and Demons', 'Atlas Shrugged', 'Dune', 'Harry Potter', 'Ulysses']
Removing items from a set
| Method | What it does |
|---|---|
remove(item) | Removes the item. Raises KeyError if it isn’t there. |
discard(item) | Removes the item if it’s there. Never raises an error. |
pop() | Removes and returns an arbitrary item. Raises KeyError if the set is empty. |
clear() | Removes all items. |
remove() vs. discard()
Use remove() when the item must be there (a missing item is a bug you want to notice). Use discard() when it may or may not be there:
Set operations
Python supports the classic mathematical set operations, each with an operator and an equivalent method. The examples use these two sets:
| Operation | Operator | Method | Result for a, b |
|---|---|---|---|
| Union (in either) | a | b | a.union(b) | {1, 2, 3, 4, 5, 6} |
| Intersection (in both) | a & b | a.intersection(b) | {3, 4} |
| Difference (in a, not in b) | a - b | a.difference(b) | {1, 2} |
| Symmetric difference (in exactly one) | a ^ b | a.symmetric_difference(b) | {1, 2, 5, 6} |
Operator vs. method: operators require both sides to be sets. Methods accept any iterable: a.union([7, 8]) works, but a | [7, 8] raises a TypeError.
Union

Output:
7 ['blue', 'green', 'indigo', 'orange', 'red', 'violet', 'yellow']
Intersection

Difference

Difference is not symmetric: a - b and b - a give different results.
Symmetric difference

The symmetric difference contains items that are in one set or the other, but not both. It’s the union minus the intersection.
In-place update methods
The methods above return a new set and leave the original unchanged. Each one also has an in-place version that modifies the set and returns None:
| Returns a new set | Updates in place | Augmented operator |
|---|---|---|
union() | update() | a |= b |
intersection() | intersection_update() | a &= b |
difference() | difference_update() | a -= b |
symmetric_difference() | symmetric_difference_update() | a ^= b |
Subset, superset, and disjoint sets
a.issubset(b)ora <= b: every item ofais inb. Usea < bfor a proper subset (a subset that isn’t equal).a.issuperset(b)ora >= b:acontains every item ofb.a.isdisjoint(b): the sets have no items in common.
Copying a set
Use copy() or set() to create an independent copy. Assigning with = doesn’t copy: both names refer to the same set, so a change through one name shows up in the other.
Sort a set
A set has no order, so it has no sort() method. sorted() returns a new sorted list of the set’s items. (Converting that list back to a set would lose the order again.)
Built-in functions with sets
Many built-in functions work on any iterable, including sets:
Watch out: True == 1 and False == 0 in Python, so {True, 1, 1.0} is the one-item set {True}.
Frozenset
A frozenset is an immutable set. You can’t add or remove items, but you can use all the non-updating operations (union, intersection, and so on). Because it’s immutable, a frozenset is hashable, so it can be a dictionary key or an item of another set.
Nested sets
A set can’t contain another regular set, because sets aren’t hashable. To build a set of sets, use frozensets as the inner sets:
Set comprehension
A set comprehension builds a set in one expression, with optional filtering. It’s like a list comprehension with curly braces, and duplicates are removed automatically:
{expression for item in iterable if condition}Code language: Python (python)
When to use a set
- Remove duplicates from a collection.
- Fast membership tests, such as checking whether a user ID is in a large collection of banned IDs.
- Compare collections: find common items, missing items, or items unique to one side.
Use a list instead when order matters or duplicates are meaningful, and a dictionary when each item needs an associated value.
Summary of set methods
| Method | Description |
|---|---|
add(x) | Add one item |
update(*iterables) | Add items from iterables |
remove(x) / discard(x) | Remove an item (with / without KeyError) |
pop() / clear() | Remove an arbitrary item / all items |
union(), intersection(), difference(), symmetric_difference() | Return a new set |
*_update() versions | Modify the set in place |
issubset(), issuperset(), isdisjoint() | Compare two sets |
copy() | Return a shallow copy |
Practice: Solve the Python set exercise and take the Python set quiz.
