A Set Is an Unordered Bag of Unique Items
Where lists and tuples care about order, sets don't. Where lists let duplicates stack up, sets silently drop them. The set is the right data structure when you have two specific needs:
- You want unique items, full stop.
- You want to check membership fast.
Write one with curly braces:
The duplicate "red" and "green" in the second set just disappear. That's not an error — it's the whole point.
Creating Sets
The two ways you'll actually use:
That last point catches everyone once: {} creates an empty dictionary, not an empty set. The syntactic ambiguity had to land on one of them, and dicts won.
Adding and Removing
remove vs discard is the key distinction: remove insists the item be present; discard doesn't care. Pick based on whether the absence of the item should be an error.
Fast Membership
This is where sets really earn their place. x in some_set runs in constant time, no matter how big the set is. x in some_list has to walk the list, which gets slow when the list is large.
Rule of thumb: any time you find yourself writing if x in some_list inside a loop, and the list has more than a few dozen items, convert the list to a set first.
Set Math
This is where sets get genuinely fun. You can combine them with operators that mirror mathematical set operations:
Each operator has a method form too (.union(), .intersection(), .difference(), .symmetric_difference()). The operators are more compact; the method forms accept any iterable, not just another set.
Deduplicating a List
One of the most common uses of sets, even outside of "set logic":
One line, dupes gone. One thing to note: the order is not preserved. If you need uniqueness and the original order, use dict.fromkeys() instead:
Dicts preserve insertion order in modern Python, and dict.fromkeys builds one using the iterable's items as keys — effectively an ordered set.
Subsets and Supersets
Checking whether one set is contained in another:
These come up in things like permissions checks ("does this user have all required roles?").
What Can Go in a Set
Only hashable items. That's a technical term — in practice, it means:
- Immutable things are hashable: numbers, strings, tuples of hashables, frozensets.
- Mutable things aren't: lists, dicts, other sets can't be set members.
If you need a set of sets, use frozenset — it's an immutable version of set.
Iteration Is Unordered
Looping over a set gives you items in no guaranteed order:
Run that a few times and you'll see the order vary. If order matters, a set isn't the right structure — sort the contents when you need them, or use a list.
When Not to Use a Set
If any of these are true, a list or dict is probably a better fit:
- You care about order.
- You need to store duplicates.
- Each item has associated data (use a dict with the item as the key).
Moving On
Sets handle uniqueness and membership. Dictionaries — coming next — handle the broader "lookup a value by a key" pattern, which is probably the most useful non-list data structure in Python.
Frequently Asked Questions
What is a Python set?
A set is an unordered collection of unique items. Sets use curly braces with no key-value pairs: colors = {'red', 'green', 'blue'}. Adding the same value twice has no effect — duplicates are silently dropped.
When should I use a set instead of a list?
Use a set when you care about uniqueness or when you'll be doing many membership checks (x in collection). Sets remove duplicates automatically and check membership in constant time, which for large collections is a huge speedup over lists.
How do I create an empty set in Python?
Use set(), not {} — curly braces with nothing inside create an empty dictionary, not an empty set. Once you have a set, you can add items with .add(value).