Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python Set Type: Syntax, Operations, and Performance

Learn how the Python set type works: creation syntax, set operators and methods, hash-based membership testing, performance tradeoffs, and common errors.

~4 min readPython
#python sets#set operations#data structures#membership testing
Illustration showing duplicate shapes merging into unique elements, representing the Python set type's deduplication behavior.

The Python set type is a built-in collection that stores unique, hashable elements in an unordered structure. Unlike lists or tuples, a set guarantees that no element appears more than once, and it provides near-constant-time membership testing because it is implemented as a hash table.

What Defines the Python Set Type

A set is defined by three properties: uniqueness, unorderedness, and hashability requirements.

Uniqueness means that adding an element that already exists in the set is a no-op. The set does not store duplicates, and this is enforced by the hash-based storage mechanism rather than by an explicit comparison against every existing element.

Unorderedness means that iteration order is not specified. For string elements in CPython, the order can vary between runs because of hash randomization controlled by PYTHONHASHSEED; in any case, code should not depend on iteration order. You cannot index into a set, and you cannot rely on the order in which elements were inserted.

Hashability means every element must be hashable. Mutable containers like lists and dictionaries cannot be elements, but tuples can, provided their contents are also hashable.

Creating Sets and the Empty Set Trap

The most common way to create a set is with curly braces:

python
colors = {"red", "green", "blue"}

There is a subtle trap here: empty curly braces create a dict, not a set.

python
empty = {} # dict empty_set = set() # set

This mismatch is a common source of bugs. If you need an empty set, you must call set() rather than using {}. You can also create a set from any iterable:

python
unique_words = set(["cat", "dog", "cat", "bird"]) # {"cat", "dog", "bird"}

Set comprehensions follow the same syntax as list comprehensions but with braces:

python
squares = {x * x for x in range(5)} # {0, 1, 4, 9, 16}

Set Operations: Operators vs Methods

Sets support both operators and methods for the core collection operations.

OperationOperatorMethod
Uniona | ba.union(b)
Intersectiona & ba.intersection(b)
Differencea - ba.difference(b)
Symmetric differencea ^ ba.symmetric_difference(b)

The binary operators listed here require both operands to be sets. The corresponding methods accept any iterable:

python
a = {1, 2, 3} b = {3, 4, 5} result = a | b # {1, 2, 3, 4, 5} result = a.union([3, 4, 5, 6]) # {1, 2, 3, 4, 5, 6}

Trying a | [3, 4] raises a TypeError, but a.union([3, 4]) works. This distinction matters when you are mixing sets with other iterable types. In-place operators such as |=, &=, -=, and ^= mutate the set in place instead of returning a new one, which avoids allocating a second set when the original is no longer needed.

Membership Testing and Hash-Based Lookup

The primary reason to reach for a set is membership testing. Checking whether an element exists in a set is O(1) on average because of the hash table implementation:

python
if "admin" in allowed_roles: grant_access()

The in operator computes the hash of the element and looks up the corresponding bucket directly, the same mechanism that makes dictionary key lookups fast. Scanning a list for the same purpose is O(n), so for large collections where membership checks happen frequently, the difference is substantial.

When to Use a Set Instead of a List

Sets and lists serve different purposes. A list preserves order, allows duplicates, and supports indexing. A set provides uniqueness and fast membership testing but no ordering.

Use a set when you need to deduplicate a collection, test membership frequently, compute union or intersection between collections, or when element order does not matter. Use a list when order matters, when you need index-based access, when duplicates are meaningful, or when elements must be modified in place.

A typical deduplication pattern is:

python
duplicates = [1, 2, 2, 3, 3, 3] unique = list(set(duplicates)) # order is not preserved

If order must be preserved while deduplicating, a dict-based approach works because dicts preserve insertion order in Python 3.7 and later:

python
seen = dict.fromkeys(duplicates) ordered_unique = list(seen.keys())

Mutable Elements and Common Set Errors

Because sets rely on hashing, they can only contain hashable elements. Adding a list or a dict to a set raises an error:

python
s = set() s.add([1, 2]) # TypeError: unhashable type: 'list'

If a list's contents changed after being added, its hash would change, breaking the set's internal structure. Tuples are hashable when their contents are hashable, so they can be used as set elements.

Another common error is modifying a set while iterating over it:

python
s = {1, 2, 3} for x in s: s.remove(x) # RuntimeError: Set changed size during iteration

The fix is to iterate over a copy:

python
for x in list(s): s.remove(x)

Performance Characteristics of Set Operations

The hash-based implementation gives sets their performance profile. Membership testing, insertion, and deletion are all O(1) on average. The worst case is O(n) when many elements collide in the same hash bucket, but with CPython's hash function and dynamic resizing of the underlying table, this is rare in normal use.

For two sets a and b, union and symmetric difference generally require examining elements from both sets, so their average cost is O(len(a) + len(b)). Intersection and difference can be implemented by iterating over one set and probing the other for membership, so their cost is proportional to the number of elements examined rather than to the sum of both lengths. Memory usage is higher than a list of the same size because the hash table allocates more slots than there are elements, and each slot stores the element plus hash metadata. For small collections this overhead is negligible, but for millions of elements it becomes relevant.

One operational consideration is the frozenset type, which provides the same semantics but is immutable. Because a frozenset is hashable, it can be used as a dictionary key or as an element of another set. This is useful when you need a set-like value that must be nested inside another hash-based container, such as a set of allowed permission groups where each group is itself a fixed collection of permissions.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.