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.

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:
colors = {"red", "green", "blue"}There is a subtle trap here: empty curly braces create a dict, not a set.
empty = {} # dict
empty_set = set() # setThis 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:
unique_words = set(["cat", "dog", "cat", "bird"])
# {"cat", "dog", "bird"}Set comprehensions follow the same syntax as list comprehensions but with braces:
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.
| Operation | Operator | Method |
|---|---|---|
| Union | a | b | a.union(b) |
| Intersection | a & b | a.intersection(b) |
| Difference | a - b | a.difference(b) |
| Symmetric difference | a ^ b | a.symmetric_difference(b) |
The binary operators listed here require both operands to be sets. The corresponding methods accept any iterable:
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:
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:
duplicates = [1, 2, 2, 3, 3, 3]
unique = list(set(duplicates)) # order is not preservedIf order must be preserved while deduplicating, a dict-based approach works because dicts preserve insertion order in Python 3.7 and later:
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:
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:
s = {1, 2, 3}
for x in s:
s.remove(x) # RuntimeError: Set changed size during iterationThe fix is to iterate over a copy:
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 toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.