Python Sets: Unordered Collections of Unique Values
What you’ll learn
Section titled “What you’ll learn”- How to create sets and why
{}is not an empty set. - How to add, remove, and discard items safely.
- Set math with union, intersection, difference, and symmetric difference.
frozenset, the immutable, hashable set.- When to reach for a set: membership testing, deduplication, and set math.
- Why membership tests on sets are far faster than on lists.
Prerequisites
Section titled “Prerequisites”- Python 3.9 or newer installed (
python --version). - Familiarity with lists, dicts, and basic iteration.
A set is the right structure whenever you care about membership and uniqueness rather than position. Where a list remembers order and allows repeats, a set answers “is this value present?” in constant time and collapses duplicates on the way in. The OrdersPortal system uses sets to deduplicate a noisy order feed, to compare what two warehouses stock, and to validate an incoming status against a fixed set of allowed values. This tutorial builds each of those from the language’s set primitives.
Step 1 — Creating sets
Section titled “Step 1 — Creating sets”A set is an unordered collection of unique, hashable items. Duplicates are discarded the moment you build the set, which makes a set the natural tool for “the distinct values from this list.” There are two ways to make a set: the {1, 2, 3} literal and the set() constructor. The literal cannot make an empty set, because {} is the dict literal — a common first bug.
order_ids = {"ORD-001", "ORD-002", "ORD-001"} # duplicate droppedprint(order_ids) # {'ORD-001', 'ORD-002'} — order not guaranteed
# Build a set from any iterable, e.g. a list with duplicatesraw = ["ORD-001", "ORD-002", "ORD-002", "ORD-003"]unique = set(raw)print(unique) # {'ORD-001', 'ORD-002', 'ORD-003'}
# Empty set — must use set(), not {}empty = set()print(type(empty)) # <class 'set'>
not_a_set = {}print(type(not_a_set)) # <class 'dict'>Verify: type({}) is dict, not set, while type(set()) is set. That confirms {} cannot create an empty set.
Step 2 — Adding and removing items
Section titled “Step 2 — Adding and removing items”add inserts a single item. remove deletes an item and raises KeyError if it is missing, while discard deletes an item silently if absent, which is usually what you want. pop removes and returns an arbitrary item, and clear empties the set. Sets are unordered, so iteration order is not the insertion order.
shipped = {"ORD-001", "ORD-002"}
shipped.add("ORD-003")print(shipped) # {'ORD-001', 'ORD-002', 'ORD-003'} (order varies)
shipped.discard("ORD-009") # no error even though it is absentshipped.remove("ORD-001")print("ORD-001" in shipped) # False
arbitrary = shipped.pop() # removes some item — you do not control whichprint(shipped) # the remaining itemsVerify: Call remove("ORD-009") on a set that does not contain it. It raises KeyError, while discard("ORD-009") on the same set does nothing. That shows the difference between the two.
Step 3 — Union, intersection, and difference
Section titled “Step 3 — Union, intersection, and difference”Sets support the classic set operations with both operators and methods. Union (| or .union()) combines two sets. Intersection (& or .intersection()) keeps items present in both. Difference (- or .difference()) keeps items in the first set but not the second. The operators require both operands to be sets; the methods accept any iterable, so .union(some_list) works where | some_list would fail.
north_customers = {"BensonCo", "NovaCorp", "Acme"}south_customers = {"NovaCorp", "Acme", "Zenith"}
all_customers = north_customers | south_customers # unionprint(all_customers) # {'BensonCo', 'NovaCorp', 'Acme', 'Zenith'}
shared = north_customers & south_customers # intersectionprint(shared) # {'NovaCorp', 'Acme'}
north_only = north_customers - south_customers # differenceprint(north_only) # {'BensonCo'}
# Methods accept any iterable, not just a setsouth_only = south_customers.difference(["Acme", "NovaCorp"])print(south_only) # {'Zenith'}Verify: Union of two sets that share two customers should produce a four-item set, not five — the shared customers appear once, confirming duplicates are dropped.
Step 4 — Symmetric difference and updates
Section titled “Step 4 — Symmetric difference and updates”Symmetric difference (^ or .symmetric_difference()) keeps the items that are in exactly one of the two sets — the “either, but not both” operation. The in-place update operators (|=, &=, -=, ^=) modify a set rather than building a new one, which matters when you want to keep the same object.
north = {"BensonCo", "NovaCorp", "Acme"}south = {"NovaCorp", "Acme", "Zenith"}
either_but_not_both = north ^ southprint(either_but_not_both) # {'BensonCo', 'Zenith'}
# In-place update: keep only what north and south sharenorth &= southprint(north) # {'NovaCorp', 'Acme'} — north is now the intersectionVerify: Symmetric difference returns the customers that belong to only one region, never both. north ^= south would give the same result as either_but_not_both, then leave north holding that set.
Step 5 — frozenset
Section titled “Step 5 — frozenset”A frozenset is a set you cannot change after creation: no add, remove, or discard. Because it is immutable and therefore hashable, a frozenset can itself be an element of another set or a key in a dict, which an ordinary set cannot be. Use it when you need set math on a value that must stay stable, such as a cached set of valid order statuses.
valid_statuses = frozenset({"pending", "paid", "shipped", "cancelled"})
# A dict keyed by frozensets of permissions — illegal with a plain setrole_perms = { frozenset({"read"}): "viewer", frozenset({"read", "write"}): "editor",}
print("shipped" in valid_statuses) # Trueprint("deleted" in valid_statuses) # Falseprint(role_perms[frozenset({"read"})]) # viewerVerify: Try valid_statuses.add("returned"). It raises AttributeError, confirming a frozenset is immutable, and that is exactly why it can be a dict key.
Step 6 — When to use a set
Section titled “Step 6 — When to use a set”Three situations call for a set. First, membership testing: asking “is this order ID in our records” is fast on a set. Second, deduplication: set(items) removes duplicates in one line. Third, set math: customers in both regions, items in one warehouse but not another. The cost is that sets are unordered and cannot hold unhashable items like lists, and they do not keep count of duplicates.
orders = ["ORD-001", "ORD-002", "ORD-001", "ORD-003", "ORD-002"]distinct = set(orders)print(len(distinct)) # 3 — three unique orders, deduplicated
pending = {"ORD-001", "ORD-003"}shipped = {"ORD-002", "ORD-003"}print(pending & shipped) # {'ORD-003'} — an order in both lists is a data bugVerify: len(set(orders)) is less than len(orders) whenever the list had duplicates. The drop in count is the deduplication working.
Step 7 — Performance vs lists for membership
Section titled “Step 7 — Performance vs lists for membership”Sets are backed by a hash table, so x in my_set is average O(1) — constant time regardless of size. The same test on a list is O(n), because Python must scan every item. The difference is invisible on a dozen items but dominates on thousands or more. When the same membership check runs in a loop, or the collection grows large, convert the list to a set once and test against that.
import timeit
order_ids = [f"ORD-{i:03d}" for i in range(10_000)] # 10,000 IDsid_set = set(order_ids)target = "ORD-09999"
list_time = timeit.timeit(lambda: target in order_ids, number=10_000)set_time = timeit.timeit(lambda: target in id_set, number=10_000)print(f"list membership: {list_time:.4f}s")print(f"set membership: {set_time:.4f}s")# list membership: ~0.3s (varies)# set membership: ~0.0005s (varies) — hundreds of times fasterVerify: The set test should be far faster than the list test for the same data. The exact numbers vary by machine, but the ratio is what matters: set membership is constant-time, list membership scans every element.
Complete working example
Section titled “Complete working example”Save as sets_demo.py and run with python sets_demo.py (Python 3.9+):
"""OrdersPortal: deduplicate orders, find shared customers, and check flags."""
# Deduplicate a noisy order feedraw_orders = ["ORD-001", "ORD-002", "ORD-001", "ORD-003", "ORD-002", "ORD-004"]unique_orders = sorted(set(raw_orders))print("Unique orders:", unique_orders)# Unique orders: ['ORD-001', 'ORD-002', 'ORD-003', 'ORD-004']
# Set math across two warehousesnorth_stock = {"keyboard", "mouse", "monitor", "cable"}south_stock = {"mouse", "monitor", "stand", "adapter"}
# Sort for display — sets are unordered, so we sort for stable outputprint("Both warehouses stock:", sorted(north_stock & south_stock))# Both warehouses stock: ['monitor', 'mouse']print("Only north stocks:", sorted(north_stock - south_stock))# Only north stocks: ['cable', 'keyboard']print("Stocked by exactly one warehouse:", sorted(north_stock ^ south_stock))# Stocked by exactly one warehouse: ['adapter', 'cable', 'keyboard', 'stand']
# frozenset as a cache of valid statuses, used for fast membershipvalid_statuses = frozenset({"pending", "paid", "shipped", "cancelled"})incoming = ["paid", "shipped", "deleted", "pending"]for status in incoming: print(f"{status}: {'valid' if status in valid_statuses else 'invalid'}")# paid: valid# shipped: valid# deleted: invalid# pending: validUnique orders: ['ORD-001', 'ORD-002', 'ORD-003', 'ORD-004']Both warehouses stock: ['monitor', 'mouse']Only north stocks: ['cable', 'keyboard']Stocked by exactly one warehouse: ['adapter', 'cable', 'keyboard', 'stand']paid: validshipped: validdeleted: invalidpending: validCommon mistakes
Section titled “Common mistakes”- Creating an empty set with
{}. That makes a dict. Useset(). removeon a missing item. It raisesKeyError. Usediscardwhen the item may be absent.- Expecting set order. Sets are unordered; never rely on iteration order. If order matters, sort the output or use a list.
- Putting unhashable items in a set. Lists and dicts cannot be set members or set elements. Use a tuple or a frozenset instead.
- Confusing
-with\from other languages. Set difference is-, not backslash.
Next steps
Section titled “Next steps”- Data structures tutorial — list, tuple, dict, and set compared
- Dictionaries — the other hash-based collection
- Tuples — hashable, ordered, the right set member when order matters
Related pages
Section titled “Related pages”- Lists — when order and duplicates matter more than speed
- Operators — the
| & - ^operators used for set math - Iterators and generators — sets are iterable
- collections —
Counteranddefaultdictfor related work