What a set gives up, and what it buys
Write a set with braces:
seen = {"red", "green", "red", "blue"}
Three items come out, not four: duplicates collapse silently. Print it twice and the order may differ. There is no order to preserve.
In exchange, x in some_set is roughly constant time. On a list, the same question is a scan: Python looks at each element until it finds a match or runs out. On a few dozen items no one notices. On two hundred thousand, the difference is milliseconds against microseconds. The second program on this page measures it rather than asserting it.
A set is the right container whenever your question is "have I seen this before?" or "which items appear in both groups?" Lists can answer those questions too, but they answer them slowly. Every membership test on a list walks through the list from start to finish. A set uses a hash table, so the answer comes back in roughly the same time whether the set holds ten items or ten million. That speed difference is not academic. Processing a log file, deduplicating user input, or checking permissions against an allowed list are all everyday tasks where a set turns a slow program into a fast one.
Sets also give you built-in operators for comparing two groups. Intersection tells you what two sets share. Union tells you everything in either. Difference tells you what one has that the other lacks. Symmetric difference tells you the items that appear in one but not both. Each of those is a single operator, not a loop you write and test yourself.
The trade-off is that sets have no order and no indexing. You cannot ask for the third element. If you need items in a particular sequence, a set is the wrong pick. If you need fast membership and group comparison, nothing else comes close. This page walks through building sets, deduplicating data, the four main set operators, and the performance gap between set lookup and list scanning.
Deduplicating
The most common use is dropping duplicates:
unique = set(votes)
If you need a list back, wrap it: list(set(votes)). If you need the first order kept, a set won't do it. Use list(dict.fromkeys(votes)). Dicts do preserve insertion order.
The algebra
Four operators compare two sets, and they read like the English:
python & sql # in both
python | sql # in either
python - sql # in the first only
python ^ sql # in just one
Each replaces a loop with an if inside it. "Which users have both skills" is one character. Writing it as a loop isn't wrong, it's just more code to read and more places to make a mistake.
The empty-set trap
{} is an empty dict. It has to be. Dicts claimed the braces first. An empty set is set(). This is worth remembering because {} looks just like what you want and behaves nothing like it.
When not to use one
If order matters, if duplicates are real, or if you need to index by spot, use a list. A set answers one question extremely well. "is this in here?". And refuses most others.
Order, and why you cannot rely on it
A set has no order. Printing one twice in the same run often gives the same setup, which tempts people into based on it, and that setup can change between runs or between versions. If you need a stable order, sort on the way out:
for name in sorted(unique_names):
...
If you need to keep the order things first appeared, a set is the wrong tool:
list(dict.fromkeys(items)) # de-duplicated, original order kept
Dicts preserve insertion order, so this is the standard trick for "unique but in the order I saw them".
Modifying a set
seen.add("x") # one item
seen.update(others) # many
seen.discard("x") # remove, no error if absent
seen.remove("x") # remove, KeyError if absent
discard and drop differ only in what happens when the item isn't there, and choosing between them is a small design choice: drop says "this should have been present", discard says "get rid of it if it's".
The operators have in-place forms too: a |= b adds all from b, a &= b keeps only what both share.
A frozen set, for when a set needs to be a key
Sets are mutable, so a set can't be a dict key or a member of another set. frozenset is the immutable version:
groups = {frozenset({"ana", "bo"}): "pair one"}
This comes up when the key is truly a collection - a set of tags, a group of users - and you need to look a thing up by it.
The cost, concretely
Building a set from a list is one pass. Converting is cheap. The win comes when you test membership more than once. Converting a list to a set to do a single lookup is slower than just scanning the list. Converting once and then testing thousands of times is the case the second program on this page measures, and there the difference isn't subtle.
What "hashable" is really asking
A set decides where to store an item by finding its hash. The hash has to stay the same for if the item is in the set. That is the entire reason lists and dicts can't be set members: they can change, the hash would change with them, and the set would be looking in the wrong place for a thing it definitely holds.
This isn't a rule Python invented to be awkward. It falls straight out of how the lookup works. The same constraint applies to dict keys, which is why the two rules are always taught together, and why the error message mentions hashability rather than mutability.
Strings, numbers, booleans, None and tuples of those are all hashable and can go in a set. any of it you can mutate can't.
Sets in everyday code
Three patterns cover most real uses.
Deduplication is the obvious one: set(values) merges repeats in a single pass, and wrapping it in sorted() gives back a predictable order.
Membership testing is the key one. Any time a loop holds if x in a thing, ask what a thing is. If it's a list that doesn't change during the loop, converting it to a set once before the loop turns a scan into a lookup, and that single change is often the entire fix for a slow function.
Checking two collections is the one people forget exists. "Which users are in both groups", "which needed fields are missing", "which files changed" are all one operator rather than a loop with an if inside it, and the operator version is far harder to get subtly wrong.
Comparing two collections, concretely
The operators turn four common questions into four characters:
a = {"ana", "bo", "cy"}
b = {"bo", "cy", "di"}
print(sorted(a & b))
print(sorted(a - b))
print(sorted(a ^ b))
print(a <= b, {"bo"} <= a, a.isdisjoint({"zz"}))
['bo', 'cy']
['ana']
['ana', 'di']
False True True
& is "in both", - is "in the first only", ^ is "in just one". Each replaces a loop holding an if, and each is far harder to get subtly wrong than the loop would be. There's no index, no accumulator, and no chance of testing the wrong direction.
sorted() around each result is doing real work: a set has no order, so printing one right gives an setup you must not depend on. Sorting on the way out is how you get a stable, readable result.
The last line shows the check operators. <= is "is a subset of", so {"bo"} <= a asks whether each member of the left is in the right. >= is the superset direction, and isdisjoint asks whether two sets share nothing — which is cheaper than building the intersection just to see if it's empty.
The questions each operator answers
The algebra is worth reading as English, because that is how you'll recognise which one you want.
"Which do they've in common?" is a & b. Shared tags, users in both groups, fields present in two records.
"What is in the new one that wasn't in the old one?" is new - old. Added files, new permissions, keys that appeared. Reverse the operands for what was removed, and note that this is the operator people most often get the wrong way round.
"What changed either way?" is a ^ b. It's the union of both differences, and it's the right answer for "what isn't the same". This Is often what a diff wants.
"Is all needed really present?" is needed <= given, or equivalently needed - given being empty — and the second version is more handy because it tells you *which* are missing rather than just that some are.
"Do these share at all?" is a.isdisjoint(b). This Stops at the first shared item rather than finding the whole intersection.
All five work with the method forms too — intersection, difference, symmetric_difference, issubset — and the methods accept any iterable, where the operators need both sides to be sets. a & [1, 2] raises; a.intersection([1, 2]) doesn't.
Where a set fits among the collections
Choosing a container is choosing what you give up, and stating it as a table makes the choice quick.
A list keeps order and duplicates and allows indexing. Membership testing scans. A tuple is the same and immutable. It can be a key. A set has no order, no duplicates and no indexing. Membership is constant time. A dict is a set of keys with a value attached to each, and it inherits the set's lookup speed for keys.
The choice often comes down to one question: what will you ask this collection most often? If the answer is "give me item three" or "what order did these arrive in", it's a list. If the answer is "is this one in here", it's a set. If the answer is "what is the value for this name", it's a dict.
The case worth noticing is a list that is only ever asked "is this in here?". That is a set wearing the wrong type, and converting it once before a loop changes a scan per iteration into a lookup per iteration. It's one of the very few speed changes that is both trivial to make and often decisive.
The reverse mistake is reaching for a set when duplicates carry meaning. Counting votes, recording events, keeping a history — the duplicates *are* the data, and a set silently deletes them.
Building one efficiently
Three ways to get a set, and the differences matter once the input is large.
set(iterable) is the direct conversion and the one to use when you already have the items. It's a single pass, and it works on any iterable including a gen, so set(line.strip() for line in f) never builds the mid list.
A set comprehension, {f(x) for x in items}, builds and deduplicates in the same pass. This is better than set([f(x) for x in items]), which makes the whole list first and then throws it away. The same waste as wrapping any comprehension in a converter.
Adding in a loop with add is right when the items arrive one at a time or the loop is doing a thing else as well. update takes an iterable and adds all of it, so a loop that only calls add is often one update in disguise.
The one to avoid is again and again rebuilding: seen = seen | {x} inside a loop creates a whole new set on each iteration. This Turns a linear job into a quadratic one. seen.add(x) modifies in place, and seen |= other does the same for a batch.
Questions people ask about Sets and Set Operations
Why is {} an empty dict? Dicts had the braces first. set() is the only way to write an empty set.
Can a set hold a list? No. Members must be hashable, and lists can change. Use a tuple, or a frozenset for a set of sets.
Does set() preserve order? No. Use list(dict.fromkeys(items)) to deduplicate while keeping first-seen order.
Is x in a_set really constant time? On average, yes. It hashes once and looks in one place, even so of size.
Can I sort a set? sorted(s) returns a list. A set itself has no order to arrange.
What is the difference between drop and discard? drop raises KeyError when the item is absent. discard doesn't.
Are 1 and True different set members? No. They hash the same and compare equal, so {1, True} has one element.
Can I use a set as a dict value? Yes. Only keys need to be hashable. Values can be any of it.
How do I find duplicates rather than drop them? Compare the lengths, or use collections.Counter and keep the entries with a count above one. A set alone tells you they existed, not which.
Is a set faster than a dict for membership? They use the same mechanism, so in effect no. Use a dict when you also need a value attached.
Sets and Set Operations: recap in one screen
- A set gives up order and duplicates and buys constant-time membership tests.
&, |, - and ^ answer "both", "either", "only the first" and "just one" without a loop.{} is an empty dict. set() is the empty set.- Members must be hashable, for the same reason dict keys must be: the hash decides where the item is stored.
- A list that is only ever asked "is this in here?" wants to be a set, and the conversion is often the whole fix for a slow loop.